Java File
[ Java Stream](#)
* * *
In Java, the File class is part of the `java.io` package and is used to handle file and directory pathnames.
The Java File class represents file and directory pathnames in an abstract way. The File class is primarily used for file and directory creation, file searching, and file deletion, etc.
A File object represents an actual file or directory on the disk. You can create a File object using the following constructors.
Creates a new File instance from a parent abstract pathname and a child pathname string:
File file = new File(File parent, String child);
Creates a new File instance by converting the given pathname string into an abstract pathname:
File file = new File(String pathname);
Creates a new File instance from a parent pathname string and a child pathname string.
File file = new File(String parent, String child);
Creates a new File instance by converting the given `file:` URI into an abstract pathname.
File file = new File(URI uri)
!(#)
## Example
// Create via path string
File file1 =new File("C:/test/test.txt");
// Create via parent path and child path
File file2 =new File("C:/test", "test.txt");
// Create via parent File object and child path
File parent =new File("C:/test");
File file3 =new File(parent, "test.txt");
After successfully creating a File object, you can use the methods in the following list to operate on the file.
| No. | Method Description |
| --- | --- |
| 1 | **[public String getName()](#)** Returns the name of the file or directory denoted by this abstract pathname. |
| 2 | **[public String getParent()](#)** 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()](#)** 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()](#)** Converts this abstract pathname into a pathname string. |
| 5 | **[public boolean isAbsolute()](#)** Tests whether this abstract pathname is absolute. |
| 6 | **[public String getAbsolutePath()](#)** Returns the absolute pathname string of this abstract pathname. |
| 7 | **[public boolean canRead()](#)** Tests whether the application can read the file denoted by this abstract pathname. |
| 8 | **[public boolean canWrite()](#)** Tests whether the application can modify the file denoted by this abstract pathname. |
| 9 | **[public boolean exists()](#)** Tests whether the file or directory denoted by this abstract pathname exists. |
| 10 | **[public boolean isDirectory()](#)** Tests whether the file denoted by this abstract pathname is a directory. |
| 11 | **[public boolean isFile()](#)** Tests whether the file denoted by this abstract pathname is a normal file. |
| 12 | **[public long lastModified()](#)** Returns the last-modified time of the file denoted by this abstract pathname. |
| 13 | **[public long length()](#)** Returns the length of the file denoted by this abstract pathname. |
| 14 | **[public boolean createNewFile() throws IOException](#)** Atomically creates a new, empty file named by this abstract pathname if a file with this name does not yet exist. |
| 15 | **[public boolean delete()](#)** Deletes the file or directory denoted by this abstract pathname. |
| 16 | **[public void deleteOnExit()](#)** Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates. |
| 17 | **[public String[] list()](#)** Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname. |
| 18 | **[public 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. |
| 19 | **[public File[] listFiles()](#)** Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. |
| 20 | **[public 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. |
| 21 | **[public boolean mkdir()](#)** Creates the directory named by this abstract pathname. |
| 22 | **[public boolean mkdirs()](#)** Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. |
| 23 | **[public boolean renameTo(File dest)](#)** Renames the file denoted by this abstract pathname. |
| 24 | **[public boolean setLastModified(long time)](#)** Sets the last-modified time of the file or directory named by this abstract pathname. |
| 25 | **[public boolean setReadOnly()](#)** Marks the file or directory named by this abstract pathname so that only read operations are allowed. |
| 26 | **[public static File createTempFile(String prefix, String suffix, File directory) throws IOException](#)** 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](#)** 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)](#)** Compares two abstract pathnames lexicographically. |
| 29 | **[public int compareTo(Object o)](#)** Compares this abstract pathname to another object lexicographically. |
| 30 | **[public boolean equals(Object obj)](#)** Tests this abstract pathname for equality with the given object. |
| 31 | **[public String toString()](#)** 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); if(f.isDirectory()){System.out.println(s + " is a directory"); }else{System.out.println(s + " is a file"); }}}else{System.out.println(dirname + " is not a directory"); }}}
The compilation and execution result of the above example is as follows:
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
* * Java Stream](#)
YouTip