Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.io

Class File

java.lang.Object
|
+--java.io.File

All Implemented Interfaces:

Serializable, Comparable


public class File

extends Object

implements Serializable, Comparable

This class represents a file or directory on a local disk. It provides facilities for dealing with a variety of systems that use various types of path separators ("/" versus "\", for example). It also contains method useful for creating and deleting files and directories.

Author:

Field Summary

static java.lang.StringpathSeparator

This is the string that is used to separate the host name from the path name in paths than include the host name.
static charpathSeparatorChar

This is the first character of the string used to separate the host name from the path name in paths that include a host.
static java.lang.Stringseparator

This is the path separator string for the current host.
static charseparatorChar

This is the first character of the file separator string.

Constructor Summary

File(java.io.File directory, java.lang.String name)

This method initializes a new File object to represent a file in the specified directory.
File(java.lang.String dirname, java.lang.String name)

This method initializes a new File object to represent a file in the specified named directory.
File(java.lang.String name)

This method initializes a new File object to represent a file with the specified path.

Method Summary

booleancanRead()

This method tests whether or not the current thread is allowed to to read the file pointed to by this object.
booleancanWrite()

This method test whether or not the current thread is allowed to write to this object.
intcompareTo(java.lang.Object obj)

This method compares the specified Object to this one to test for equality.
intcompareTo(java.io.File file)

This method compares the specified File to this one to test for equality.
booleancreateNewFile()

This method creates a new file of zero length with the same name as the path of this File object if an only if that file does not already exist.
static java.io.FilecreateTempFile(java.lang.String prefix, java.lang.String suffix)

This method creates a temporary file in the system temporary directory.
static synchronized java.io.FilecreateTempFile(java.lang.String prefix, java.lang.String suffix, java.io.File directory)

This method creates a temporary file in the specified directory.
synchronized booleandelete()

This method deletes the file represented by this object.
voiddeleteOnExit()

Calling this method requests that the file represented by this object be deleted when the virtual machine exits.
booleanequals(java.lang.Object obj)

This method tests two File objects for equality by comparing the path of the specified File against the path of this object.
booleanexists()

This method tests whether or not the file represented by the object actually exists on the filesystem.
java.io.FilegetAbsoluteFile()

This method returns a File object representing the absolute path of this object.
java.lang.StringgetAbsolutePath()

This method returns the path of this file as an absolute path name.
java.io.FilegetCanonicalFile()

This method returns a File object representing the canonical path of this object.
java.lang.StringgetCanonicalPath()

This method returns a canonical representation of the pathname of this file.
java.lang.StringgetName()

This method returns the name of the file.
java.lang.StringgetParent()

This method returns a String the represents this file's parent.
java.io.FilegetParentFile()

This method returns a File object representing the parent file of this one.
java.lang.StringgetPath()

Returns the path name that represents this file.
inthashCode()

This method returns a hash code representing this file.
booleanisAbsolute()

This method returns true if this object represents an absolute file path and false if it does not.
booleanisDirectory()

This method tests whether or not the file represented by this object is a directory.
booleanisFile()

This method tests whether or not the file represented by this object is a "plain" file.
booleanisHidden()

This method tests whether or not this file represents a "hidden" file.
longlastModified()

This method returns the last modification time of this file.
longlength()

This method returns the length of the file represented by this object, or 0 if the specified file does not exist.
java.lang.String[]list()

This method returns a array of String's representing the list of files is then directory represented by this object.
java.lang.String[]list(java.io.FilenameFilter filter)

This method returns a array of String's representing the list of files is then directory represented by this object.
java.io.File[]listFiles()

This method returns an array of File objects representing all the files in the directory represented by this object.
java.io.File[]listFiles(java.io.FilenameFilter filter)

This method returns an array of File objects representing all the files in the directory represented by this object.
java.io.File[]listFiles(java.io.FileFilter filter)

This method returns an array of File objects representing all the files in the directory represented by this object.
static java.io.File[]listRoots()

This method returns an array of filesystem roots.
booleanmkdir()

This method creates a directory for the path represented by this object.
booleanmkdirs()

This method creates a directory for the path represented by this file.
synchronized booleanrenameTo(java.io.File dest)

This method renames the file represented by this object to the path of the file represented by the argument File.
booleansetLastModified(long time)

This method sets the modification time on the file to the specified value.
booleansetReadOnly()

This method sets the file represented by this object to be read only.
java.lang.StringtoString()

This method returns a String that is the path name of the file as returned by getPath.
java.net.URLtoURL()

This method returns a URL with the file: protocol that represents this file.

Field Details

pathSeparator

public static final String pathSeparator

This is the string that is used to separate the host name from the path name in paths than include the host name. It is the value of the path.separator system property.


pathSeparatorChar

public static final char pathSeparatorChar

This is the first character of the string used to separate the host name from the path name in paths that include a host. The separator string is taken from the path.separator system property.


separator

public static final String separator

This is the path separator string for the current host. This field contains the value of the file.separator system property. An example separator string would be "/" on the GNU system.


separatorChar

public static final char separatorChar

This is the first character of the file separator string. On many hosts (for example, on the GNU system), this represents the entire separator string. The complete separator string is obtained from the file.separatorsystem property.


Constructor Details

File

public File(java.io.File directory, java.lang.String name)

This method initializes a new File object to represent a file in the specified directory. If the directory argument is null, the file is assumed to be in the current directory as specified by the user.dir system property

Parameters:


File

public File(java.lang.String name)

This method initializes a new File object to represent a file with the specified path.

Parameters:


File

public File(java.lang.String dirname, java.lang.String name)

This method initializes a new File object to represent a file in the specified named directory. The path name to the file will be the directory name plus the separator string plus the file name. If the directory path name ends in the separator string, another separator string will still be appended.

Parameters:


Method Details

canRead

public boolean canRead()

This method tests whether or not the current thread is allowed to to read the file pointed to by this object. This will be true if and and only if 1) the file exists and 2) the SecurityManager (if any) allows access to the file via it's checkRead method 3) the file is readable.

Returns:

Throws:


canWrite

public boolean canWrite()

This method test whether or not the current thread is allowed to write to this object. This will be true if and only if 1) The SecurityManager (if any) allows write access to the file and 2) The file exists and 3) The file is writable. To determine whether or not a non-existent file can be created, check the parent directory for write access.

Returns:

Throws:


compareTo

public int compareTo(java.io.File file)

This method compares the specified File to this one to test for equality. It does this by comparing the canonical path names of the files.

The canonical paths of the files are determined by calling the getCanonicalPath method on each object.

This method returns a 0 if the specified Object is equal to this one, a negative value if it is less than this one a positive value if it is greater than this one.

Parameters:

Returns:


compareTo

public int compareTo(java.lang.Object obj)

This method compares the specified Object to this one to test for equality. It does this by comparing the canonical path names of the files. This method is identical to compareTo(File) except that if the Object passed to it is not a File, it throws a ClassCastException

The canonical paths of the files are determined by calling the getCanonicalPath method on each object.

This method returns a 0 if the specified Object is equal to this one, a negative value if it is less than this one a positive value if it is greater than this one.

Parameters:

Returns:

Throws:


createNewFile

public boolean createNewFile()

This method creates a new file of zero length with the same name as the path of this File object if an only if that file does not already exist.

A SecurityManagercheckWrite check is done prior to performing this action.

Returns:

Throws:


createTempFile

public static File createTempFile(java.lang.String prefix, java.lang.String suffix)

This method creates a temporary file in the system temporary directory. The files created are guaranteed not to currently exist and the same file name will never be used twice in the same virtual machine instance. The system temporary directory is determined by examinging the java.io.tmpdir system property.

The prefix parameter is a sequence of at least three characters that are used as the start of the generated filename. The suffix parameter is a sequence of characters that is used to terminate the file name. This parameter may be null and if it is, the suffix defaults to ".tmp".

If a SecurityManager exists, then its checkWrite method is used to verify that this operation is permitted.

This method is identical to calling createTempFile(prefix, suffix, null).

Parameters:

Throws:


createTempFile

public static synchronized File createTempFile(java.lang.String prefix, java.lang.String suffix, java.io.File directory)

This method creates a temporary file in the specified directory. If the directory name is null, then this method uses the system temporary directory. The files created are guaranteed not to currently exist and the same file name will never be used twice in the same virtual machine instance. The system temporary directory is determined by examinging the java.io.tmpdir system property.

The prefix parameter is a sequence of at least three characters that are used as the start of the generated filename. The suffix parameter is a sequence of characters that is used to terminate the file name. This parameter may be null and if it is, the suffix defaults to ".tmp".

If a SecurityManager exists, then its checkWrite method is used to verify that this operation is permitted.

Parameters:

Throws:


delete

public synchronized boolean delete()

This method deletes the file represented by this object. If this file is a directory, it must be empty in order for the delete to succeed.

Returns:

Throws:


deleteOnExit

public void deleteOnExit()

Calling this method requests that the file represented by this object be deleted when the virtual machine exits. Note that this request cannot be cancelled. Also, it will only be carried out if the virtual machine exits normally.

Throws:


equals

public boolean equals(java.lang.Object obj)

This method tests two File objects for equality by comparing the path of the specified File against the path of this object. The two objects are equal if an only if 1) The argument is not null 2) The argument is a File object and 3) The path of the Fileargument is equal to the path of this object.

The paths of the files are determined by calling the getPath() method on each object.

Parameters:

Returns:


exists

public boolean exists()

This method tests whether or not the file represented by the object actually exists on the filesystem.

Returns:

Throws:


getAbsoluteFile

public File getAbsoluteFile()

This method returns a File object representing the absolute path of this object.

Returns:


getAbsolutePath

public String getAbsolutePath()

This method returns the path of this file as an absolute path name. If the path name is already absolute, then it is returned. Otherwise the value returned is the current directory plus the separatory string plus the path of the file. The current directory is determined from the user.dir system property.

Returns:


getCanonicalFile

public File getCanonicalFile()

This method returns a File object representing the canonical path of this object.

Returns:

Throws:


getCanonicalPath

public String getCanonicalPath()

This method returns a canonical representation of the pathname of this file. The actual form of the canonical representation is different. On the GNU system, the canonical form differs from the absolute form in that all relative file references to "." and ".." are resolved and removed.

Note that this method, unlike the other methods which return path names, can throw an IOException. This is because native method might be required in order to resolve the canonical path

Throws:


getName

public String getName()

This method returns the name of the file. This is everything in the complete path of the file after the last instance of the separator string.

Returns:


getParent

public String getParent()

This method returns a String the represents this file's parent. null is returned if the file has no parent. The parent is determined via a simple operation which removes the

Returns:


getParentFile

public File getParentFile()

This method returns a File object representing the parent file of this one.


getPath

public String getPath()

Returns the path name that represents this file. May be a relative or an absolute path name

Returns:


hashCode

public int hashCode()

This method returns a hash code representing this file. It is the hash code of the path of this file (as returned by getPath()) exclusived or-ed with the value 1234321.

Returns:


isAbsolute

public boolean isAbsolute()

This method returns true if this object represents an absolute file path and false if it does not. The definition of an absolute path varies by system. As an example, on GNU systems, a path is absolute if it starts with a "/".

Returns:


isDirectory

public boolean isDirectory()

This method tests whether or not the file represented by this object is a directory. In order for this method to return true, the file represented by this object must exist and be a directory.

Returns:

Throws:


isFile

public boolean isFile()

This method tests whether or not the file represented by this object is a "plain" file. A file is a plain file if and only if it 1) Exists, 2) Is not a directory or other type of special file.

Returns:

Throws:


isHidden

public boolean isHidden()

This method tests whether or not this file represents a "hidden" file. On GNU systems, a file is hidden if its name begins with a "." character. Files with these names are traditionally not shown with directory listing tools.

Returns:


lastModified

public long lastModified()

This method returns the last modification time of this file. The time value returned is an abstract value that should not be interpreted as a specified time value. It is only useful for comparing to other such time values returned on the same system. In that case, the larger value indicates a more recent modification time.

If the file does not exist, then a value of 0 is returned.

Returns:

Throws:


length

public long length()

This method returns the length of the file represented by this object, or 0 if the specified file does not exist.

Returns:

Throws:


list

public String[] list()

This method returns a array of String's representing the list of files is then directory represented by this object. If this object represents a non-directory file or a non-existent file, then null is returned. The list of files will not contain any names such as "." or ".." which indicate the current or parent directory. Also, the names are not guaranteed to be sorted.

A SecurityManager check is made prior to reading the directory. If read access to the directory is denied, an exception will be thrown.

Returns:

Throws:


list

public String[] list(java.io.FilenameFilter filter)

This method returns a array of String's representing the list of files is then directory represented by this object. If this object represents a non-directory file or a non-existent file, then null is returned. The list of files will not contain any names such as "." or ".." which indicate the current or parent directory. Also, the names are not guaranteed to be sorted.

In this form of the list() method, a filter is specified that allows the caller to control which files are returned in the list. The FilenameFilter specified is called for each file returned to determine whether or not that file should be included in the list.

A SecurityManager check is made prior to reading the directory. If read access to the directory is denied, an exception will be thrown.

Parameters:

Returns:

Throws:


listFiles

public File[] listFiles()

This method returns an array of File objects representing all the files in the directory represented by this object. If this object does not represent a directory, null is returned. Each of the returned File object is constructed with this object as its parent.

A SecurityManager check is made prior to reading the directory. If read access to the directory is denied, an exception will be thrown.

Returns:

Throws:


listFiles

public File[] listFiles(java.io.FileFilter filter)

This method returns an array of File objects representing all the files in the directory represented by this object. If this object does not represent a directory, null is returned. Each of the returned File object is constructed with this object as its parent.

In this form of the listFiles() method, a filter is specified that allows the caller to control which files are returned in the list. The FileFilter specified is called for each file returned to determine whether or not that file should be included in the list.

A SecurityManager check is made prior to reading the directory. If read access to the directory is denied, an exception will be thrown.

Parameters:

Returns:

Throws:


listFiles

public File[] listFiles(java.io.FilenameFilter filter)

This method returns an array of File objects representing all the files in the directory represented by this object. If this object does not represent a directory, null is returned. Each of the returned File object is constructed with this object as its parent.

In this form of the listFiles() method, a filter is specified that allows the caller to control which files are returned in the list. The FilenameFilter specified is called for each file returned to determine whether or not that file should be included in the list.

A SecurityManager check is made prior to reading the directory. If read access to the directory is denied, an exception will be thrown.

Parameters:

Returns:

Throws:


listRoots

public static File[] listRoots()

This method returns an array of filesystem roots. Some operating systems have volume oriented filesystem. This method provides a mechanism for determining which volumes exist. GNU systems use a single hierarchical filesystem, so will have only one "/" filesystem root.

Returns:


mkdir

public boolean mkdir()

This method creates a directory for the path represented by this object.

Returns:

Throws:


mkdirs

public boolean mkdirs()

This method creates a directory for the path represented by this file. It will also create any intervening parent directories if necessary.

Returns:

Throws:


renameTo

public synchronized boolean renameTo(java.io.File dest)

This method renames the file represented by this object to the path of the file represented by the argument File.

Parameters:

Returns:

Throws:


setLastModified

public boolean setLastModified(long time)

This method sets the modification time on the file to the specified value. This is specified as the number of seconds since midnight on January 1, 1970 GMT.

Parameters:

Returns:

Throws:


setReadOnly

public boolean setReadOnly()

This method sets the file represented by this object to be read only. A read only file or directory cannot be modified. Please note that GNU systems allow read only files to be deleted if the directory it is contained in is writable.

Returns:

Throws:


toString

public String toString()

This method returns a String that is the path name of the file as returned by getPath.

Returns:


toURL

public URL toURL()

This method returns a URL with the file: protocol that represents this file. The exact form of this URL is system dependent.

Returns:

Throws: