java.io.File

The File class of java.io package, is an abstract representation of file and directory structures.  If you are looking for class to handle file and directory operations, the File class suits this requirements. Let’s take a look a more detailed functionality of File class.

In real life applications, most of the user interfaces and operating systems use system-dependent pathname string with regards to naming files and directories. To illustrate further a fully qualified pathname in unix looks like ‘/apps/java/test.txt’ while in windows is somewhat similar to ‘D:javatest.txt’. Did you notice the difference? The File class offer a system independent view of hierarchical pathnames.

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 separator and separatorChar 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.

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 property user.dir, and is typically the directory in which the Java virtual machine was invoked.

In getting the parent of an abstract pathname, the getParent() method is available. 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”.

File Class Syntax

public class File
extends Object
implements Serializable, Comparable

File Class Compatibility Version

StringTokenizer Class is available since Java 1.0

Basic Usage of File

The File class as part of the java.io package is one of the classes of the java api that is widely used because it provides mechanism in accessing Files. It has methods to create, delete and modify file attributes. This is an abstract representation of a physical file on the system. This class is practically the most important method of java as most of the Reader classes such as Scanner, FileReader are all relying to File class to represent a physical file. For example

Scanner s = new Scanner(new File(“/tmp/test.xt”));

File Method Usage Examples

The following are the detailed list of File methods and descriptions. We have also provided links to examples of each method on the list.

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, 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.
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 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 a java.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()
Deprecated.
This method does not automatically escape characters that are illegal in URLs. It is recommended that new code convert an abstract pathname into a URL by first converting it into a URI, via the toURI method, and then converting the URI into a URL via the URI.toURL method.