import java.io.*;

class TestFileClass {

    public static void main(String[] args) throws IOException {
    
        new File("test.txt");               // does not create a new file
        
        // create a file in the current directory
        File f = new File("test1.txt");
        System.out.println( f.createNewFile() );
        
        // create a file one directory up from the current directory
        File f1 = new File("..", "test2.txt");
        f1.createNewFile();
        
        System.out.println( f1.getAbsolutePath() );
        System.out.println( f1.getCanonicalPath() );
        
        // rename a file
        File f2 = new File("testRename.txt");
        f.renameTo(f2);     // renames the file on the system
        System.out.println( f.getAbsolutePath() );  // returns original filename
        System.out.println( "Does old file exist?: " + f.exists() );                
        System.out.println( "Does new file exist?: " + f2.exists() );   
        
        // toURL() - constructs a valid URL for the file
        System.out.println( f.toURL() ); 
        
        // listFiles()
        System.out.println("\n.txt Files \n");
        File dir = new File("d://Java/jeg/io");
        MyFileFilter mff = new MyFileFilter(".txt");
        File [] txt = dir.listFiles(mff);
        if( txt == null ) {
            System.out.println("Can't get a list of .txt files.");
        } else {
        for( int i=0; i<txt.length; i++ )
            System.out.println( txt[i] );
        }    
        
        // listRoots() example from Java Class Libraries, 2nd Edition Supplement
        // Displays available drives
        System.out.println("\nRoots: \n");
        File[] roots = File.listRoots();
        if( roots == null ) {
            System.out.println("Cannot get roots.");
        } else {
            for( int i=0; i<roots.length; i++) 
                System.out.println(roots[i]);
        }
    }

    
    static class MyFileFilter implements FileFilter {
        String suffix;
        
        MyFileFilter(String suffix) {
            this.suffix = suffix;
        }
        
        public boolean accept(File target) {
            return target.getName().endsWith(suffix);
        }
    }
}
