Java File Class
The Java File class represents file and directory path names in an abstract manner. This class is primarily used for creating files and directories, searching for files, and deleting files.
A File object represents an actual file or directory on a disk. A File object can be created using the following constructors:
Creates a new File instance from a parent abstract pathname and a child pathname string.
File(File parent, String child);
Creates a new File instance by converting the given pathname string into an abstract pathname.
File(String pathname)
Creates a new File instance from a parent pathname string and a child pathname string.
File(String parent, String child)
Creates a new File instance by converting the given file: URI into an abstract pathname.
File(URI uri)
After successfully creating a File object, the following methods can be used to manipulate the file.
Number | Method Description |
---|---|
1 | public String getName() <br>Returns the name of the file or directory denoted by this abstract pathname. |
2 | public String getParent() <br>Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory. |
3 | public File getParentFile() <br>Returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not name a parent directory. |
4 | public String getPath() <br>Converts this abstract pathname into a pathname string. |
5 | public boolean isAbsolute() <br>Tests whether this abstract pathname is absolute. |
6 | public String getAbsolutePath() <br>Returns the absolute pathname string of this abstract pathname. |
7 | public boolean canRead() <br>Tests whether the application can read the file denoted by this abstract pathname. |
8 | public boolean canWrite() <br>Tests whether the application can modify the file denoted by this abstract pathname. |
9 | public boolean exists() <br>Tests whether the file or directory denoted by this abstract pathname exists. |
10 | public boolean isDirectory() <br>Tests whether the file denoted by this abstract pathname is a directory. |
11 | public boolean isFile() <br>Tests whether the file denoted by this abstract pathname is a normal file. |
12 | public long lastModified() <br>Returns the time that the file denoted by this abstract pathname was last modified. |
13 | public long length() <br>Returns the length of the file denoted by this abstract pathname. |
14 | public boolean createNewFile() throws IOException <br>Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. |
15 | public boolean delete() <br>Deletes the file or directory denoted by this abstract pathname. |
16 | public void deleteOnExit() <br>Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates. |
17 | public String[] list() <br>Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname. |
18 | public String[] list(FilenameFilter filter) <br>Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. |
19 | public File[] listFiles() <br>Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. |
20 | public File[] listFiles(FileFilter filter) <br>Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. |
21 | public boolean mkdir() <br>Creates the directory named by this abstract pathname. |
22 | public boolean mkdirs() <br>Creates the directory specified by this abstract pathname, including any necessary but nonexistent parent directories. |
23 | public boolean renameTo(File dest) <br>Renames the file denoted by this abstract pathname. |
24 | public boolean setLastModified(long time) <br>Sets the last modification time of the file or directory specified by this abstract pathname. |
25 | public boolean setReadOnly() <br>Marks the file or directory specified by this abstract pathname so that only read operations are allowed. |
26 | public static File createTempFile(String prefix, String suffix, File directory) throws IOException <br>Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. |
27 | public static File createTempFile(String prefix, String suffix) throws IOException <br>Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name. |
28 | public int compareTo(File pathname) <br>Compares two abstract pathnames lexicographically. |
29 | public int compareTo(Object o) <br>Compares the abstract pathname with the given object lexicographically. |
30 | public boolean equals(Object obj) <br>Tests this abstract pathname for equality with the given object. |
31 | public String toString() <br>Returns the pathname string of this abstract pathname. |
Example
The following example demonstrates the use of the File object:
Example
import java.io.File;
public class DirList {
public static void main(String args[]) {
String dirname = "/java";
File f1 = new File(dirname);
if (f1.isDirectory()) {
System.out.println("Directory of " + dirname);
String s[] = f1.list();
for (int i = 0; i < s.length; i++) {
File f = new File(dirname + "/" + s[i]);
if (f.isDirectory()) {
System.out.println(s[i] + " is a directory");
} else {
System.out.println(s[i] + " is a file");
}
}
} else {
System.out.println(dirname + " is not a directory");
}
}
}
The above example compiles and runs with the following result:
Directory of /java
bin is a directory
lib is a directory
demo is a directory
test.txt is a file
README is a file
index.html is a file
include is a directory