FileTest.java

import java.io.*;
import java.net.*; // for URL

class FileTest
{
    public static void main(String argv[])
    {
        int i;

        // Enumerate the root(s) of the file system.
        System.out.println();
        System.out.println("Filesystem roots:");
        File[] roots = File.listRoots();
        for (i=0; i<roots.length; i++)
            System.out.println("   " + roots[i]);

        // Play with canonical pathnames.
        System.out.println();
        String messy = "../othercode/./././Filetest.java";
        try {
            File mFile = new File(messy);
            System.out.println("Messy relative path:");
            System.out.println("   " + messy);
            System.out.println("Clean absolute path:");
            System.out.println("   " + mFile.getCanonicalPath());
            }
        catch(IOException e) {
            System.out.println(
                "Path '" + messy +"' has an error: " + e);
            }

        // Try the toURL() method.
        System.out.println();
        String me = "./Filetest.java";
        try {
            File xFile = new File(me);
            System.out.println("Filename:");
            System.out.println("   " + xFile.getCanonicalPath());
            URL url = xFile.toURL();
            System.out.println("URL:");
            System.out.println("   " + url);
            URI uri = xFile.toURI();
            System.out.println("URI:");
            System.out.println("   " + uri);
            }
        catch(IOException e) {
            System.out.println("Bad filename or URL: " + e);
            }

        // Try to create a directory.
        System.out.println();
        File nested = new File("./nested1/nested2/nested3");
        if (nested.exists())
            System.out.println("Directory '" + 
                nested.getName() + "' already exists!");
        else if (nested.mkdirs()) {
            String real;
            try {
                real = nested.getCanonicalPath();
                }
            catch(IOException e) {
                real = nested.getName();
                }
            System.out.println("Created directory '" + real + "'.");
            }
        else
            System.out.println("Couldn't create '" + 
                nested.getName() + "'!");

        // List all source files in the current directory.
        System.out.println();
        File cur = new File(".");
        File[] flist = cur.listFiles(new SourceFilter());
        System.out.println("Source files in current directory:");
        for (i=0; i<flist.length; i++)
            if (flist[i].isFile())
                System.out.println("   " + 
                    flist[i].getName() + " -- " + 
                    flist[i].length() + " bytes");

        // Copy a file.
        try {
            int c;
            System.out.println();
            String inName = "FileTest.java";
            String outName = "FileTest.java.copy";
            File f = new File(inName);
            FileInputStream inStream = new FileInputStream(f);
            FileOutputStream outStream = new FileOutputStream(outName);
            while (true) {
                c = inStream.read();
                if (c == -1)
                    break;
                outStream.write(c);
                }
            inStream.close();
            outStream.close();
            System.out.println("Copied '" + 
                inName + "' to '" + outName + "'.");
            }
        catch(IOException e) {
            System.err.println("Couldn't copy file: " + e);
            }
    }
}


/**
Accept only 'source' file names.
*/
class SourceFilter implements FilenameFilter
{
    public boolean accept(File dir,String name)
    {
        for (int i=0; i<extensions.length; i++)
            if (name.toLowerCase().endsWith(extensions[i]))
                return true;
        return false;
    }
    private String[] extensions = {
        ".java",
        ".py",
        ".cs",
        ".c",
        ".h",
        ".html",
        };
}