Package java.io Previous
Previous
Java API
Java API
Index
Index
Next
Next

Class File

Fields , Constructors , Methods , Footnotes

public  class  java.io.File
    extends  java.lang.Object  
{

        // Fields
    public final static String pathSeparator;	
    public final static char pathSeparatorChar;	
    public final static String separator;	
    public final static char separatorChar;	

        // Constructors
    public File(File  dir, String  name);	
    public File(String  path);	
    public File(String  path, String  name);	



        // Methods
    public boolean canRead();	
    public boolean canWrite();	
    public boolean delete();	
    public boolean equals(Object  obj);	
    public boolean exists();	
    public String getAbsolutePath();	
    public String getName();	
    public String getParent();	
    public String getPath();	
    public int hashCode();	
    public boolean isAbsolute();	
    public boolean isDirectory();	
    public boolean isFile();	
    public long lastModified();	
    public long length();	
    public String[] list();	
    public String[] list(FilenameFilter  filter);	
    public boolean mkdir();	
    public boolean mkdirs();	
    public boolean renameTo(File  dest);	
    public String toString();	
}

Instances of this class represent the name of a file or directory on the host file system. A file is specified by a path name, which can either be an absolute path name or a path name relative to the current working directory. The path name must follow the naming conventions of the host platform.

The File class is intended to provide an abstraction that deals with most of the machine- dependent complexities of files and path names in a machine-independent fashion.


Fields


pathSeparator

public final static String pathSeparator 

The system-dependent path separator string. This field is initialized to contain the value of the system property "path.-separator".


pathSeparatorChar

public final static char pathSeparatorChar 

The system-dependent path separator character. This field is initialized to contain the first character of the value of the system property "path.separator". This character is often used to separate file names in a sequence of files given as a "path list".


separator

public final static String separator 

The system-dependent path separator string. This field is initialized to contain the value of the system property "file.-separator".


separatorChar

public final static char separatorChar 

The system-dependent path separator string. This field is initialized to contain the first character of the value of the system property "file.separator".This character separates the directory and file components in a file name.


Constructors


File

public File(File  dir, String  name) 

Creates a File instance that represents the file with the specified name in the specified directory.

If the directory argument is null. the resulting File instance represents a file in the (system-dependent) current directory whose path name is the name argument. Otherwise, the File instance represents a file whose path name is the pathname of the directory, followed by the separator character , followed by the name argument.

ParameterDescription
dir the directory
name the file path name


File

public File(String  path) 

Creates a File instance that represents the file whose path name is the given path argument.

ParameterDescription
path the file path name

Throw:

NullPointerException

If the file path is equal to null.


File

public File(String  path, String  name) 

Creates a File instance whose path name is the pathname of the specified directory, followed by the separator character , followed by the name argument.

ParameterDescription
path the directory path name
name the file path name


Methods


canRead

public boolean canRead() 

Determines if the application can read from the specified file.

Return Value:

Returns true if the file specified by this object exists and the application can read the file; false otherwise

Throw:

SecurityException

If a security manager exists, its checkRead method is called with the path name of this File to see if the application is allowed read access to the file.


canWrite

public boolean canWrite() 

Determines if the application can write to this file.

Return Value:

Returns true if the application is allowed to write to a file whose name is specified by this object; false otherwise

Throw:

SecurityException

If a security manager exists, its checkWrite method is called with the path name of this File to see if the application is allowed write access to the file.


delete

public boolean delete() 

Deletes the file specified by this object.

Return Value:

Returns true if the file is successfully deleted; false otherwise.

Throw:

SecurityException

If a security manager exists, its checkDelete method is called with with the path name of this File to see if the application is allowed to delete the file.


equals

public boolean equals(Object  obj) 

The result is true if and only if the argument is not null and is a File object whose path name is equal to the path name of this object.

Return Value:

Returns true if the objects are the same; false otherwise.

ParameterDescription
obj the object to compare with

Overrides:

equals in class Object .


exists

public boolean exists() 

Determines if this File exists.

Return Value:

Returns true if the file specified by this object exists; false otherwise

Throw:

SecurityException

If a security manager exists, its checkRead method is called with the path name of this File to see if the application is allowed read access to the file.


getAbsolutePath

public String getAbsolutePath() 

If this object represents an absolute path name , then return the path name . Otherwise, return a path name that is a concatenation of the current user directory, the separator character, and the path name of this file object.

The system property user.dir contains the current user directory.

Return Value:

Returns a system-dependent absolute path name for this File.


getName

public String getName() 

Returns the name of the file represented by this object. The name is everything in the path name after the last occurrence of the separator character .

Return Value:

Returns the name of the file (without any directory components) represented by this File object.


getParent

public String getParent() 

Returns the parent directory of the file represented by this object. The parent directory is everything in the path name before the last occurrence of the separator character , or null if the separator character does not appear in the path name.

Return Value:

Returns the name of the directory of the file represented by this File object.


getPath

public String getPath() 

Return Value:

Returns the path name represented by this File object.


hashCode

public int hashCode() 

Return Value:

Returns a hash code value for this File object.

Overrides:

hashCode in class Object .


isAbsolute

public boolean isAbsolute() 

Determines if this File represents an absolute path name. The definition of an absolute path name is system-dependent.(1)

Return Value:

Returns true if the path name indicated by the File object is an absolute path name; false otherwise.


isDirectory

public boolean isDirectory() 

Determines if the file represented by this File object is a directory.

Return Value:

Returns true if this File exists and is a directory; false otherwise.

Throw:

SecurityException

If a security manager exists, its checkRead method is called with the path name of this File to see if the application is allowed read access to the file.


isFile

public boolean isFile() 

Determines if the file represented by this File object is a "normal" file.

A file is "normal" if it is not a directory and, in addition, satisfies other system-dependent criteria. Any non-directory file created by a Java application is guaranteed to be a normal file.

Return Value:

Returns true if the file specified by this object exists and is a "normal" file; false otherwise.

Throw:

SecurityException

If a security manager exists, its checkRead method is called with the path name of this File to see if the application is allowed read access to the file.


lastModified

public long lastModified() 

Determines the time that the file represented by this File object was last modified.

The return value is system-dependent and should only be used to compare with other values returned by last modified. It should not be interpreted as an absolute time.

Return Value:

Returns the time the file specified by this object was last modified, or 0L if the specified file does not exist.

Throw:

SecurityException

If a security manager exists, its checkRead method is called with the path name of this File to see if the application is allowed read access to the file.


length

public long length() 

Determines the length of the file represented by this File object.

Return Value:

Returns the length, in bytes, of the file specified by this object, or 0L if the specified file does not exist.

Throw:

SecurityException

If a security manager exists, its checkRead method is called with the path name of this File to see if the application is allowed read access to the file.


list

public String[] list() 

Lists the files in the directory specified by this File.

Return Value:

Returns an array of file names in the specified directory. This list does not include the current directory or the parent directory ("." and ".." on Unix systems).

Throw:

SecurityException

If a security manager exists, its checkRead method is called with the path name of this File to see if the application is allowed read access to the file.


list

public String[] list(FilenameFilter  filter) 

Lists the files in the directory specified by this File that satisfy the specified filter .

Return Value:

Returns an array of file names in the specified directory that satisfy the filter. This list does not include the current directory or the parent directory ("." and ".." on Unix systems).

ParameterDescription
filter a filename filter.

Throw:

SecurityException

If a security manager exists, its checkRead method is called with the path name of this File to see if the application is allowed read access to the file.


mkdir

public boolean mkdir() 

Creates a directory whose path name is specified by this File object.

Return Value:

Returns true if the directory could be created; false otherwise

Throw:

SecurityException

If a security manager exists, its checkWrite method is called with the path name of this File to see if the application is allowed write access to the file.


mkdirs

public boolean mkdirs() 

Creates a directory whose path name is specified by this File object. In addition, create all parent directories as necessary.

Return Value:

Returns true if the directory (or directories) could be created; false otherwise.

Throw:

SecurityException

If a security manager exists, its checkWrite method is called with the path name of each of the directories that is to be created, before any of the directores are created.


renameTo

public boolean renameTo(File  dest) 

Renames the file specified by this File object to have the path name given by the File argument.

Return Value:

Returns true if the renaming succeeds; false otherwise.

ParameterDescription
dest the new file name

Throw:

SecurityException

If a security manager exists, its checkWrite method is called both with the path name of this file object and with the path name of the destination target object to see if the application is allowed to write to both files.


toString

public String toString() 

Returns a string representation of this object.

Return Value:

Returns a string giving the path name of this object.

Overrides:

toString in class Object .


Footnotes

(1)For example, on Unix, a path name is absolute if its first character is the separator character . On Windows platforms, an path name is absolute if its first character is an ASCII '\', '/', or if it begins with a letter followed by a colon.

Top© 1996 Sun Microsystems, Inc. All rights reserved.