Team LiB
Previous Section Next Section

Filejava.io

Java 1.0serializable comparable

This class supports a platform-independent definition of file and directory names. It also provides methods to list the files in a directory; check the existence, readability, writability, type, size, and modification time of files and directories; make new directories; rename files and directories; delete files and directories; and create and delete temporary and lock files. The constants defined by this class are the platform-dependent directory and path-separator characters, available as a String and a char.

getName( ) returns the name of the File with any directory names omitted. getPath( ) returns the full name of the file, including the directory name. getParent( ) and getParentFile( ) return the directory that contains the File; the only difference between the two methods is that one returns a String, while the other returns a File. isAbsolute( ) tests whether the File is an absolute specification. If not, getAbsolutePath( ) returns an absolute filename created by appending the relative filename to the current working directory. getAbsoluteFile( ) returns the equivalent absolute File object. getCanonicalPath( ) and getCanonicalFile( ) are similar methods: they return an absolute filename or File object that has been converted to its system-dependent canonical form. This can be useful when comparing two File objects to see if they refer to the same file or directory. In Java 1.4 and later, the toURI( ) method returns a java.net.URI object that uses a file: scheme to name this file. This file-to-URI transformation can be reversed by passing a file: URI object to the File( ) constructor.

exists( ), canWrite( ), canRead( ), isFile( ), isDirectory( ), and isHidden( ) perform the obvious tests on the specified File. length( ) returns the length of the file. lastModified( ) returns the modification time of the file (which should be used for comparison with other file times only and not interpreted as any particular time format). setLastModified( ) allows the modification time to be set; setReadOnly( ) makes a file or directory read-only.

list( ) returns the names of all entries in a directory that are not rejected by an optional FilenameFilter. listFiles( ) returns an array of File objects that represent all entries in a directory not rejected by an optional FilenameFilter or FileFilter. listRoots( ) returns an array of File objects representing all root directories on the system. Unix systems typically have only one root, /. Windows systems have a different root for each drive letter: c:\, d:\, and e:\, for example.

mkdir( ) creates a directory, and mkdirs( ) creates all the directories in a File specification. renameTo( ) renames a file or directory; delete( ) deletes a file or directory. Prior to Java 1.2, the File class doesn't provide any way to create a file; that task is accomplished typically with FileOutputStream. Two special-purpose file creation methods have were added in Java 1.2. The static createTempFile( ) method returns a File object that refers to a newly created empty file with a unique name that begins with the specified prefix (which must be at least three characters long) and ends with the specified suffix. One version of this method creates the file in a specified directory, and the other creates it in the system temporary directory. Applications can use temporary files for any purpose without worrying about overwriting files belonging to other applications. The other file-creation method of Java 1.2 is createNewFile( ). This instance method attempts to create a new, empty file with the name specified by the File object. If it succeeds, it returns TRue. However, if the file already exists, it returns false. createNewFile( ) works atomically and is therefore useful for file locking and other mutual-exclusion schemes. When working with createTempFile( ) or createNewFile( ), consider using deleteOnExit( ) to request that the files be deleted when the Java VM exits normally.

Figure 9-14. java.io.File


public class File implements Serializable, Comparable<File> {
// Public Constructors
1.4  public File(java.net.URI uri);  
     public File(String pathname);  
     public File(File parent, String child);  
     public File(String parent, String child);  
// Public Constants
     public static final String pathSeparator;  
     public static final char pathSeparatorChar;  
     public static final String separator;  
     public static final char separatorChar;  
// Public Class Methods
1.2  public static File createTempFile(String prefix, String suffix) throws IOException;  
1.2  public static File createTempFile(String prefix, String suffix, File directory) throws 
     IOException;
1.2  public static File[ ] listRoots( );  
// Public Instance Methods
     public boolean canRead( );  
     public boolean canWrite( );  
1.2  public boolean createNewFile( ) throws IOException;  
     public boolean delete( );  
1.2  public void deleteOnExit( );  
     public boolean exists( );  
1.2  public File getAbsoluteFile( );  
     public String getAbsolutePath( );  
1.2  public File getCanonicalFile( ) throws IOException;  
1.1  public String getCanonicalPath( ) throws IOException;  
     public String getName( );  
     public String getParent( );  
1.2  public File getParentFile( );  
     public String getPath( );  
     public boolean isAbsolute( );  
     public boolean isDirectory( );  
     public boolean isFile( );  
1.2  public boolean isHidden( );  
     public long lastModified( );  
     public long length( );  
     public String[ ] list( );  
     public String[ ] list(FilenameFilter filter);  
1.2  public File[ ] listFiles( );  
1.2  public File[ ] listFiles(FilenameFilter filter);  
1.2  public File[ ] listFiles(FileFilter filter);  
     public boolean mkdir( );  
     public boolean mkdirs( );  
     public boolean renameTo(File dest);  
1.2  public boolean setLastModified(long time);  
1.2  public boolean setReadOnly( );  
1.4  public java.net.URI toURI( );  
1.2  public java.net.URL toURL( ) throws java.net.MalformedURLException;  
// Methods Implementing Comparable
1.2  public int compareTo(File pathname);  
// Public Methods Overriding Object
     public boolean equals(Object obj);  
     public int hashCode( );  
     public String toString( );  
}

Passed To

Too many methods to list.

Returned By

ProcessBuilder.directory( )

    Team LiB
    Previous Section Next Section