Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

java.io
Class File  view File download File.java

java.lang.Object
  extended byjava.io.File
All Implemented Interfaces:
java.lang.Comparable, Serializable

public class File
extends java.lang.Object
implements Serializable, java.lang.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.


Field Summary
private static java.lang.String dupSeparator
           
private static long last_tmp
          The time (millisecond), when the last temporary file was created.
private static int n_created
          The number of files, created during the current millisecond.
private  java.lang.String path
          This is the path to the file set when the object is created.
static java.lang.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.
static 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.
static java.lang.String separator
          This is the path separator string for the current host.
static char separatorChar
          This is the first character of the file separator string.
private static long serialVersionUID
           
 
Constructor Summary
File(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 name)
          This method initializes a new File object to represent a file with the specified path.
File(java.lang.String dirPath, java.lang.String name)
          This method initializes a new File object to represent a file in the specified named directory.
File(java.net.URI uri)
          This method initializes a new File object to represent a file corresponding to the specified file: protocol URI.
 
Method Summary
 boolean canRead()
          This method tests whether or not the current thread is allowed to to read the file pointed to by this object.
 boolean canWrite()
          This method test whether or not the current thread is allowed to write to this object.
private  void checkRead()
           
private  void checkWrite()
           
 int compareTo(File other)
          This method compares the specified File to this one to test for equality.
 int compareTo(java.lang.Object obj)
          This method compares the specified Object to this one to test for equality.
 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.
static File createTempFile(java.lang.String prefix, java.lang.String suffix)
          This method creates a temporary file in the system temporary directory.
static File createTempFile(java.lang.String prefix, java.lang.String suffix, File directory)
          This method creates a temporary file in the specified directory.
 boolean delete()
          This method deletes the file represented by this object.
 void deleteOnExit()
          Calling this method requests that the file represented by this object be deleted when the virtual machine exits.
 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.
 boolean exists()
          This method tests whether or not the file represented by the object actually exists on the filesystem.
 File getAbsoluteFile()
          This method returns a File object representing the absolute path of this object.
 java.lang.String getAbsolutePath()
          This method returns the path of this file as an absolute path name.
 File getCanonicalFile()
          This method returns a File object representing the canonical path of this object.
 java.lang.String getCanonicalPath()
          This method returns a canonical representation of the pathname of this file.
 java.lang.String getName()
          This method returns the name of the file.
 java.lang.String getParent()
          This method returns a String the represents this file's parent.
 File getParentFile()
          This method returns a File object representing the parent file of this one.
 java.lang.String getPath()
          Returns the path name that represents this file.
 int hashCode()
          This method returns a hash code representing this file.
 boolean isAbsolute()
          This method returns true if this object represents an absolute file path and false if it does not.
 boolean isDirectory()
          This method tests whether or not the file represented by this object is a directory.
 boolean isFile()
          This method tests whether or not the file represented by this object is a "plain" file.
 boolean isHidden()
          This method tests whether or not this file represents a "hidden" file.
 long lastModified()
          This method returns the last modification time of this file.
 long length()
          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(FilenameFilter filter)
          This method returns a array of String's representing the list of files is then directory represented by this object.
 File[] listFiles()
          This method returns an array of File objects representing all the files in the directory represented by this object.
 File[] listFiles(FileFilter filter)
          This method returns an array of File objects representing all the files in the directory represented by this object.
 File[] listFiles(FilenameFilter filter)
          This method returns an array of File objects representing all the files in the directory represented by this object.
static File[] listRoots()
          This method returns an array of filesystem roots.
 boolean mkdir()
          This method creates a directory for the path represented by this object.
 boolean mkdirs()
          This method creates a directory for the path represented by this file.
private  java.lang.String normalizePath(java.lang.String p)
           
private  void readObject(ObjectInputStream ois)
           
 boolean renameTo(File dest)
          This method renames the file represented by this object to the path of the file represented by the argument File.
 boolean setLastModified(long time)
          This method sets the modification time on the file to the specified value.
 boolean setReadOnly()
          This method sets the file represented by this object to be read only.
 java.lang.String toString()
          This method returns a String that is the path name of the file as returned by getPath.
 java.net.URI toURI()
           
 java.net.URL toURL()
          This method returns a URL with the file: protocol that represents this file.
private  void writeObject(ObjectOutputStream oos)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

separator

public static final java.lang.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.


dupSeparator

private static final java.lang.String dupSeparator

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.


pathSeparator

public static final java.lang.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.


path

private java.lang.String path
This is the path to the file set when the object is created. It may be an absolute or relative path name.


last_tmp

private static long last_tmp
The time (millisecond), when the last temporary file was created.


n_created

private static int n_created
The number of files, created during the current millisecond.

Constructor Detail

File

public File(java.lang.String name)
This method initializes a new File object to represent a file with the specified path.


File

public File(java.lang.String dirPath,
            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.


File

public File(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


File

public File(java.net.URI uri)
This method initializes a new File object to represent a file corresponding to the specified file: protocol URI.

Method Detail

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.


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.


createNewFile

public boolean createNewFile()
                      throws IOException
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 SecurityManager.checkWrite check is done prior to performing this action.

Since:
1.2

delete

public 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.


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.


exists

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


normalizePath

private java.lang.String normalizePath(java.lang.String p)

getAbsolutePath

public java.lang.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.


getAbsoluteFile

public File getAbsoluteFile()
This method returns a File object representing the absolute path of this object.

Since:
1.2

getCanonicalPath

public java.lang.String getCanonicalPath()
                                  throws IOException
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


getCanonicalFile

public File getCanonicalFile()
                      throws IOException
This method returns a File object representing the canonical path of this object.

Since:
1.2

getName

public java.lang.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.


getParent

public java.lang.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


getParentFile

public File getParentFile()
This method returns a File object representing the parent file of this one.

Since:
1.2

getPath

public java.lang.String getPath()
Returns the path name that represents this file. May be a relative or an absolute path name


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.


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 "/".


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.


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.


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.

Since:
1.2

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.


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.


list

public java.lang.String[] list(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.


list

public java.lang.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.


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.

Since:
1.2

listFiles

public File[] listFiles(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.

Since:
1.2

listFiles

public File[] listFiles(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.

Since:
1.2

toString

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


toURI

public java.net.URI toURI()

toURL

public java.net.URL toURL()
                   throws java.net.MalformedURLException
This method returns a URL with the file: protocol that represents this file. The exact form of this URL is system dependent.


mkdir

public boolean mkdir()
This method creates a directory for the path represented by this object.


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.


createTempFile

public static File createTempFile(java.lang.String prefix,
                                  java.lang.String suffix,
                                  File directory)
                           throws IOException
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.

Since:
1.2

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.

Since:
1.2

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.

Since:
1.2

createTempFile

public static File createTempFile(java.lang.String prefix,
                                  java.lang.String suffix)
                           throws IOException
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).


compareTo

public int compareTo(File other)
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.

Since:
1.2

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.

Specified by:
compareTo in interface java.lang.Comparable
Since:
1.2

renameTo

public boolean renameTo(File dest)
This method renames the file represented by this object to the path of the file represented by the argument File.


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.

Since:
1.2

checkWrite

private void checkWrite()

checkRead

private void checkRead()

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.

Since:
1.2

writeObject

private void writeObject(ObjectOutputStream oos)
                  throws IOException

readObject

private void readObject(ObjectInputStream ois)
                 throws java.lang.ClassNotFoundException,
                        IOException