File
File deals directly with files and the file system. File class describes the properties of a file or a directory itself.
A File object tells the information associated with a disk file, such as the permissions, time, date, and directory path, and to navigate subdirectory hierarchies.
A directory in Java is treated simply as a File with one additional property. A list of filenames that can be examined by the list( ) method.
File(String directoryPath)
- directoryPath is the path name of the file
File(String directoryPath, String filename)
- filename is the name of the file or subdirectory,
File(File dirObj, String filename)
- dirObj is a File object that specifies a directory
File(URI uriObj)
- uriObj is a URI object that describes a file.
File Object | Description |
---|---|
File f1 = new File("/"); | f1 is constructed with a directory path as the only argument. |
File f2 = new File("/","autoexec.bat"); | f2 has the path and the filename. |
File f3 = new File(f1,"autoexec.bat"); | f3 refers to the same file as f2. |
If you use a forward slash (/) on a Windows, the path will still resolve correctly. To use a backslash character (\), you will need to use its escape sequence (\\) within a string.
Constants from File class
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.
File Constructor
File(File parent, String child)
- Creates a new File instance from a parent abstract pathname and a child pathname string.
File(String pathname)
- Creates a new File instance by converting the given pathname string into an abstract pathname.
File(String parent, String child)
- Creates a new File 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.
Is it executable, readable or writable
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.
Compare two file path
int compareTo(File pathname)
- Compares two abstract pathnames lexicographically.
boolean equals(Object obj)
- Tests this abstract pathname for equality with the given object.
Create a file
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.
Delete a file
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.
Is it a file or a directory
boolean isDirectory()
- Tests whether the file denoted by this abstract pathname is a directory.
boolean isFile()
- returns true if called on a file and false if called on a directory.
Whether the file or directory denoted by this abstract pathname exists
boolean exists()
- Tests whether the file or directory denoted by this abstract pathname exists.
Whether this abstract pathname is absolute
boolean isAbsolute()
- Tests whether this abstract pathname is absolute.
Is this file hidden
boolean isHidden()
- Tests whether the file named by this abstract pathname is a hidden file.
Get the file last modified time
long lastModified()
- Returns the time that the file denoted by this abstract pathname was last modified.
Get the file size
long length()
- Returns the length of the file denoted by this abstract pathname.
Get file path and name
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.
String getName()
- Returns the name of the file or directory denoted by this abstract pathname.
String getPath()
- Converts this abstract pathname into a pathname string.
Get free space, total space, usable space
long getFreeSpace()
- Returns the number of unallocated bytes in the partition named by this abstract path name.
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.
Get parent file
String getParent()
- Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.
File getParentFile()
- Returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not name a parent directory.
Return file in a directory
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.
Create new directories
boolean mkdir()
- Creates the directory.
boolean mkdirs()
- Creates the directory, including any necessary but nonexistent parent directories.
Rename file
boolean renameTo(File dest)
- Renames the file denoted by this abstract pathname.
Change to executable, readable, writable
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.
Change last-modified time
boolean setLastModified(long time)
- Sets the last-modified time of the file or directory named by this abstract pathname.
Get string representation of a file location
String toString()
- Returns the pathname string of this abstract pathname.
Convert file location to URI and URL
URI toURI()
- Creates a file: URI that represents this abstract pathname.
Revised from Open JDK source code
Java Book
File Stream
- File
- Constants from File class
- Create file object
- Is it executable, readable or writable
- Compare two file path
- Create a file
- Delete a file
- Is it a file or a directory
- Whether the file or directory denoted by this abstract pathname exists
- Whether this abstract pathname is absolute
- Is this file hidden
- Get the file last modified time
- Get the file size
- Get file path and name
- Get free space, total space, usable space
- Get parent file
- Return file in a directory
- The file system roots
- Create new directories
- Rename file
- Change to executable, readable, writable
- Change last-modified time
- Get string representation of a file location
- Convert file location to URI and URL