- java.lang.Object
-
- java.io.File
-
- All Implemented Interfaces:
- Serializable, Comparable<File>
public class File extends Object implements Serializable, Comparable<File>
An abstract representation of file and directory pathnames.User interfaces and operating systems use system-dependent pathname strings to name files and directories. This class presents an abstract, system-independent view of hierarchical pathnames. An abstract pathname has two components:
- An optional system-dependent prefix string, such as a disk-drive specifier,
"/"
for the UNIX root directory, or"\\\\"
for a Microsoft Windows UNC pathname, and - A sequence of zero or more string names.
The conversion of a pathname string to or from an abstract pathname is inherently system-dependent. When an abstract pathname is converted into a pathname string, each name is separated from the next by a single copy of the default separator character. The default name-separator character is defined by the system property
file.separator
, and is made available in the public static fields
andseparator
of this class. When a pathname string is converted into an abstract pathname, the names within it may be separated by the default name-separator character or by any other name-separator character that is supported by the underlying system.separatorChar
A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the
java.io
package always resolve relative pathnames against the current user directory. This directory is named by the system propertyuser.dir
, and is typically the directory in which the Java virtual machine was invoked.The parent of an abstract pathname may be obtained by invoking the
getParent()
method of this class and consists of the pathname's prefix and each name in the pathname's name sequence except for the last. Each directory's absolute pathname is an ancestor of any File object with an absolute abstract pathname which begins with the directory's absolute pathname. For example, the directory denoted by the abstract pathname "/usr" is an ancestor of the directory denoted by the pathname "/usr/local/bin".The prefix concept is used to handle root directories on UNIX platforms, and drive specifiers, root directories and UNC pathnames on Microsoft Windows platforms, as follows:
- For UNIX platforms, the prefix of an absolute pathname is always
"/"
. Relative pathnames have no prefix. The abstract pathname denoting the root directory has the prefix"/"
and an empty name sequence. - For Microsoft Windows platforms, the prefix of a pathname that contains a drive specifier consists of the drive letter followed by
":"
and possibly followed by"\\"
if the pathname is absolute. The prefix of a UNC pathname is"\\\\"
; the hostname and the share name are the first two names in the name sequence. A relative pathname that does not specify a drive has no prefix.
Instances of this class may or may not denote an actual file-system object such as a file or a directory. If it does denote such an object then that object resides in a partition. A partition is an operating system-specific portion of storage for a file system. A single storage device (e.g. a physical disk-drive, flash memory, CD-ROM) may contain multiple partitions. The object, if any, will reside on the partition named by some ancestor of the absolute form of this pathname.
A file system may implement restrictions to certain operations on the actual file-system object, such as reading, writing, and executing. These restrictions are collectively known as access permissions. The file system may have multiple sets of access permissions on a single object. For example, one set may apply to the object's owner, and another may apply to all other users. The access permissions on an object may cause some methods in this class to fail.
Instances of the
File
class are immutable; that is, once created, the abstract pathname represented by aFile
object will never change.Interoperability with
java.nio.file
packageThe
java.nio.file
package defines interfaces and classes for the Java virtual machine to access files, file attributes, and file systems. This API may be used to overcome many of the limitations of thejava.io.File
class. ThetoPath
method may be used to obtain aPath
that uses the abstract path represented by aFile
object to locate a file. The resultingPath
may be used with theFiles
class to provide more efficient and extensive access to additional file operations, file attributes, and I/O exceptions to help diagnose errors when an operation on a file fails.- Since:
- JDK1.0
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field and Description static String
pathSeparator
The system-dependent path-separator character, represented as a string for convenience.static char
pathSeparatorChar
The system-dependent path-separator character.static String
separator
The system-dependent default name-separator character, represented as a string for convenience.static char
separatorChar
The system-dependent default name-separator character.
-
Constructor Summary
Constructors Constructor and Description File(File parent, String child)
Creates a newFile
instance from a parent abstract pathname and a child pathname string.File(String pathname)
Creates a newFile
instance by converting the given pathname string into an abstract pathname.File(String parent, String child)
Creates a newFile
instance from a parent pathname string and a child pathname string.File(URI uri)
Creates a new File instance by converting the given file: URI into an abstract pathname.
-
Method Summary
Methods Modifier and Type Method and Description boolean
canExecute()
Tests whether the application can execute the file denoted by this abstract pathname.boolean
canRead()
Tests whether the application can read the file denoted by this abstract pathname.boolean
canWrite()
Tests whether the application can modify the file denoted by this abstract pathname.int
compareTo(File pathname)
Compares two abstract pathnames lexicographically.boolean
createNewFile()
Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.static File
createTempFile(String prefix, String suffix)
Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.static File
createTempFile(String prefix, String suffix, File directory)
Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.boolean
delete()
Deletes the file or directory denoted by this abstract pathname.void
deleteOnExit()
Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.boolean
equals(Object obj)
Tests this abstract pathname for equality with the given object.boolean
exists()
Tests whether the file or directory denoted by this abstract pathname exists.File
getAbsoluteFile()
Returns the absolute form of this abstract pathname.String
getAbsolutePath()
Returns the absolute pathname string of this abstract pathname.File
getCanonicalFile()
Returns the canonical form of this abstract pathname.String
getCanonicalPath()
Returns the canonical pathname string of this abstract pathname.long
getFreeSpace()
Returns the number of unallocated bytes in the partition named by this abstract path name.String
getName()
Returns the name of the file or directory denoted by this abstract pathname.String
getParent()
Returns the pathname string of this abstract pathname's parent, ornull
if this pathname does not name a parent directory.File
getParentFile()
Returns the abstract pathname of this abstract pathname's parent, ornull
if this pathname does not name a parent directory.String
getPath()
Converts this abstract pathname into a pathname string.long
getTotalSpace()
Returns the size of the partition named by this abstract pathname.long
getUsableSpace()
Returns the number of bytes available to this virtual machine on the partition named by this abstract pathname.int
hashCode()
Computes a hash code for this abstract pathname.boolean
isAbsolute()
Tests whether this abstract pathname is absolute.boolean
isDirectory()
Tests whether the file denoted by this abstract pathname is a directory.boolean
isFile()
Tests whether the file denoted by this abstract pathname is a normal file.boolean
isHidden()
Tests whether the file named by this abstract pathname is a hidden file.long
lastModified()
Returns the time that the file denoted by this abstract pathname was last modified.long
length()
Returns the length of the file denoted by this abstract pathname.String[]
list()
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.String[]
list(FilenameFilter filter)
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.File[]
listFiles()
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.File[]
listFiles(FileFilter filter)
Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.File[]
listFiles(FilenameFilter filter)
Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.static File[]
listRoots()
List the available filesystem roots.boolean
mkdir()
Creates the directory named by this abstract pathname.boolean
mkdirs()
Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.boolean
renameTo(File dest)
Renames the file denoted by this abstract pathname.boolean
setExecutable(boolean executable)
A convenience method to set the owner's execute permission for this abstract pathname.boolean
setExecutable(boolean executable, boolean ownerOnly)
Sets the owner's or everybody's execute permission for this abstract pathname.boolean
setLastModified(long time)
Sets the last-modified time of the file or directory named by this abstract pathname.boolean
setReadable(boolean readable)
A convenience method to set the owner's read permission for this abstract pathname.boolean
setReadable(boolean readable, boolean ownerOnly)
Sets the owner's or everybody's read permission for this abstract pathname.boolean
setReadOnly()
Marks the file or directory named by this abstract pathname so that only read operations are allowed.boolean
setWritable(boolean writable)
A convenience method to set the owner's write permission for this abstract pathname.boolean
setWritable(boolean writable, boolean ownerOnly)
Sets the owner's or everybody's write permission for this abstract pathname.Path
toPath()
Returns ajava.nio.file.Path
object constructed from the this abstract path.String
toString()
Returns the pathname string of this abstract pathname.URI
toURI()
Constructs a file: URI that represents this abstract pathname.URL
toURL()
-
'Dev. 자바 > API 및 이론' 카테고리의 다른 글
[JAVA API] java.io.FileOutputStream (0) | 2012.08.08 |
---|---|
[JAVA API] java.io.FileInputStream (0) | 2012.08.08 |
[JAVA API] java.lang.Enum<E>, 열거형 (0) | 2012.08.08 |
[JAVA API] java.util.HashMap<K,V> (0) | 2012.08.07 |
[JAVA API] java.lang.Exception (0) | 2012.08.07 |