Packages This Package Prev Next Index
§2.7 Class File
public class java.io.File
extends java.lang.Object (I-§1.12)
{
// Fields
public final static String pathSeparator; §2.7.1
public final static char pathSeparatorChar; §2.7.2
public final static String separator; §2.7.3
public final static char separatorChar; §2.7.4
// Constructors
public File(File dir, String name); §2.7.5
public File(String path); §2.7.6
public File(String path, String name); §2.7.7
// Methods
public boolean canRead(); §2.7.8
public boolean canWrite(); §2.7.9
public boolean delete(); §2.7.10
public boolean equals(Object obj); §2.7.11
public boolean exists(); §2.7.12
public String getAbsolutePath(); §2.7.13
public String getName(); §2.7.14
public String getParent(); §2.7.15
public String getPath(); §2.7.16
public int hashCode(); §2.7.17
public boolean isAbsolute(); §2.7.18
public boolean isDirectory(); §2.7.19
public boolean isFile(); §2.7.20
public long lastModified(); §2.7.21
public long length(); §2.7.22
public String[] list(); §2.7.23
public String[] list(FilenameFilter filter); §2.7.24
public boolean mkdir(); §2.7.25
public boolean mkdirs(); §2.7.26
public boolean renameTo(File dest); §2.7.27
public String toString(); §2.7.28
}
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.
pathSeparator
public final static String pathSeparator
- The system-dependent path separator string. This field is initialized to
contain the value of the system property (I-§1.18.9) "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 (I-§1.18.9)
"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 (I-§1.18.9) "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 (I-§1.18.9)
"file.separator".This character separates the directory and file components in
a file name.
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 (I-§2.7.16)
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 (I-§2.7.3), followed by the name argument.
- Parameters:
dir
-
the directory
name
-
the file path name
File
public File(String path)
- Creates a File instance that represents the file whose path name (I-§2.7.16)
is the given path argument.
- Parameters:
path
-
the file path name
- Throws
- NullPointerException (I-§1.40)
- If the file path is equal to null.
File
public File(String path, String name)
- Creates a File instance whose path name (I-§2.7.16) is the pathname of the
specified directory, followed by the separator character (I-§2.7.3), followed by the name argument.
- Parameters:
path
-
the directory path name
name
-
the file path name
canRead
public boolean canRead()
- Determines if the application can read from the specified file.
- Returns:
- true if the file specified by this object exists and the application can
read the file; false otherwise
- Throws
- SecurityException (I-§1.43)
- If a security manager exists, its checkRead method (I-§1.15.19) is
called with the path name (I-§2.7.16) 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.
- Returns:
- true if the application is allowed to write to a file whose name is specified by this object; false otherwise
- Throws
- SecurityException (I-§1.43)
- If a security manager exists, its checkWrite method (I-§1.15.24) is
called with the path name (I-§2.7.16) 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.
- Returns:
- true if the file is successfully deleted; false otherwise.
- Throws
- SecurityException (I-§1.43)
- If a security manager exists, its checkDelete method (I-§1.15.9) is called
with with the path name (I-§2.7.16) 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.
- Parameters:
obj
-
the object to compare with
- Returns:
- true if the objects are the same; false otherwise.
- Overrides:
- equals in class Object (I-§1.12.3).
exists
public boolean exists()
- Determines if this File exists.
- Returns:
- true if the file specified by this object exists; false otherwise
- Throws
- SecurityException (I-§1.43)
- If a security manager exists, its checkRead method (I-§1.15.19) is
called with the path name (I-§2.7.16) 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 (I-§2.7.18), then return the
path name (I-§2.7.16). 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 (I-§1.18.9) user.dir contains the current user directory.
- 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 (I-§2.7.16) after the last occurrence of the separator
character (I-§2.7.3).
- 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 (I-§2.7.16) before the last
occurrence of the separator character (I-§2.7.3), or null if the separator
character does not appear in the path name.
- Returns:
- the name of the directory of the file represented by this File object.
getPath
public String getPath()
- Returns:
- the path name represented by this File object.
hashCode
public int hashCode()
- Returns:
- a hash code value for this File object.
- Overrides:
- hashCode in class Object (I-§1.12.6).
isAbsolute
public boolean isAbsolute()
- Determines if this File represents an absolute path name. The definition of
an absolute path name is system-dependent.1
- Returns:
- true if the path name (I-§2.7.16) 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.
- Returns:
- true if this File exists and is a directory; false otherwise.
- Throws
- SecurityException (I-§1.43)
- If a security manager exists, its checkRead method (I-§1.15.19) is
called with the path name (I-§2.7.16) 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.
- Returns:
- true if the file specified by this object exists and is a "normal" file; false
otherwise.
- Throws
- SecurityException (I-§1.43)
- If a security manager exists, its checkRead method (I-§1.15.19) is
called with the path name (I-§2.7.16) 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.
- Returns:
- the time the file specified by this object was last modified, or 0L if the
specified file does not exist.
- Throws
- SecurityException (I-§1.43)
- If a security manager exists, its checkRead method (I-§1.15.19) is
called with the path name (I-§2.7.16) 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.
- Returns:
- the length, in bytes, of the file specified by this object, or 0L if the
specified file does not exist.
- Throws
- SecurityException (I-§1.43)
- If a security manager exists, its checkRead method (I-§1.15.19) is
called with the path name (I-§2.7.16) 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.
- 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).
- Throws
- SecurityException (I-§1.43)
- If a security manager exists, its checkRead method (I-§1.15.19) is
called with the path name (I-§2.7.16) 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 (I-§2.26).
- Parameters:
filter
-
a filename filter.
- 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).
- Throws
- SecurityException (I-§1.43)
- If a security manager exists, its checkRead method (I-§1.15.19) is
called with the path name (I-§2.7.16) 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.
- Returns:
- true if the directory could be created; false otherwise
- Throws
- SecurityException (I-§1.43)
- If a security manager exists, its checkWrite method (I-§1.15.24) is
called with the path name (I-§2.7.16) 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.
- Returns:
- true if the directory (or directories) could be created; false otherwise.
- Throws
- SecurityException (I-§1.43)
- If a security manager exists, its checkWrite method (I-§1.15.24) is
called with the path name (I-§2.7.16) 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.
- Parameters:
dest
-
the new file name
- Returns:
- true if the renaming succeeds; false otherwise.
- Throws
- SecurityException (I-§1.43)
- If a security manager exists, its checkWrite method (I-§1.15.24) is
called both with the path name (I-§2.7.16) 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.
- Returns:
- a string giving the path name (I-§2.7.16) of this object.
- Overrides:
- toString in class Object (I-§1.12.9).
1
For example, on Unix, a path name is absolute if its first character is the separator
character (I-§2.7.3). 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.
Packages This Package Prev Next Index
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc.
All rights reserved
Please send any comments or corrections to doug.kramer@sun.com