Directory Operations

   

Although most of the methods in the File class can be used on both files and directories, the list method is only for use in a directory:

 public String[] list() 

The list method returns an array of the names of all the files contained within the directory. You can also set up a filename filter for the list method, which enables you to select only certain filenames:

 public String[] list(FilenameFilter filter) 

The FilenameFilter interface defines a single method, accept, that returns true if a filename should be included in the list:

 public abstract boolean accept(File dir, String name) 

Listing 21.7 shows an object that implements a filename filter that allows only files ending with .java.

Listing 21.7 Source Code for JavaFilter.java
 import java.io.*; // This class implements a filename filter that only allows // files that end with .java public class JavaFilter extends Object implements FilenameFilter {      public JavaFilter()      {      }      public boolean accept(File dir, String name)      { // Only return true for accept if the file ends with .java        return name.endsWith(".java");      } } 

Listing 21.8 shows a program that uses the JavaFilter to list out all the .java files in the current directory.

Listing 21.8 Source Code for JavaList.java
 import java.io.*; public class JavaList {   public static void main( String[] args )   {     if ( args.length != 1 )     {       System.out.println( "Usage: java RandomAccessExample <directory>" );       System.exit( 0 );     }     String dir = args[0];     // Create a File instance for the current directory     File currDir = new File( dir );     // Get a filtered list of the .java files in the current directory     String[] javaFiles = currDir.list( new JavaFilter() );     // Print out the contents of the javaFiles array     for (int i=0; i < javaFiles.length; i++)     {       System.out.println(javaFiles[i]);     }   } } 

Deleting Files on Exit

There is one additional feature commonly used with temporary files, but which can be applied to any file. The deleteOnExit method was added to JDK 1.2 which allows you to schedule a file to be deleted when the VM exits. This is useful if, for instance, you are creating a file within your program, but don't want to leave it on the system after the program quits.

Caution

There are two cautions you need to be aware of with this method. First, after you schedule a file to be deleted, it can't be unscheduled. The second is that the deleteOnExit method is effective only if the VM exits normally, such as by System.exit(0). If the system crashes, or there is some other form of abnormal crash, the VM will not be able to delete the file.


Listing 21.9 shows how CreateTempFile.java in Listing 21.5 would be modified to delete the file when the system exits. Notice that when you run CreateTempFile2.java, you see a file in the temp directory before the system exits, but afterwards the file is gone.

Listing 21.9 Source Code for CreateTempFile2.java
 import java.io.*; public class CreateTempFile2 {   public static void main( String[] args )   {     try     {       File tempFile = File.createTempFile( "myfile" , ".tmp" );       FileOutputStream fout = new FileOutputStream(tempFile);       PrintStream out = new PrintStream(fout); out.println("Place this test string in the temp file");       tempFile.deleteOnExit();     }     catch ( IOException ex )     {       System.out.println("There was a problem creating/writing to the temp file" );     } System.out.println("Until you hit 'Enter'there is a temp file on the system");     try     {       System.in.read();     }     catch( IOException ex )     {       ex.printStackTrace();     }     System.exit(0);   } } 
   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net