Java File Class
In Java, the File class is part of the java.io package; it is used to handle file and directory pathnames.
The Java File class represents file names and directory pathnames in an abstract way. The File class is mainly used for creating files and directories, finding files, and deleting files.
A File object represents an actually existing file or directory on disk. Create a File object using the following constructors.
Create a new File instance from the given parent abstract pathname and child pathname string:
File file = new File(File parent, String child);
Create a new File instance by converting the given pathname string into an abstract pathname:
File file = new File(String pathname);
Create a new File instance from the parent pathname string and child pathname string.
File file = new File(String parent, String child);
Create 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 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 represented by this abstract pathname. | |
| 2 | public String getParent() |
Returns the pathname string of the parent pathname of this abstract pathname, or if this pathname does not specify a parent directory. null. |
|
| 3 | public File getParentFile() |
Returns the abstract pathname of the parent pathname of this abstract pathname, or if this pathname does not specify a parent directory. null. |
|
| 4 | public String getPath() |
| Converts this abstract pathname into a pathname string. | |
| 5 | public boolean isAbsolute() |
| Tests whether this abstract pathname is an absolute pathname. | |
| 6 | public String getAbsolutePath() |
| Returns the absolute pathname string of the abstract pathname. | |
| 7 | public boolean canRead() |
| Tests whether the application can read the file represented by this abstract pathname. | |
| 8 | public boolean canWrite() |
| Tests whether the application can modify the file represented by this abstract pathname. | |
| 9 | public boolean exists() |
| Tests whether the file or directory represented by this abstract pathname exists. | |
| 10 | public boolean isDirectory() |
| Tests whether the file represented by this abstract pathname is a directory. | |
| 11 | public boolean isFile() |
| Tests whether the file represented by this abstract pathname is a normal file. | |
| 12 | public long lastModified() |
| Returns the time the file represented by this abstract pathname was last modified. | |
| 13 | public long length() |
| Returns the length of the file represented by this abstract pathname. | |
| 14 | public boolean createNewFile() throws IOException |
| Atomically creates a new empty file specified by this abstract pathname, if and only if a file with the name specified by this abstract pathname does not yet exist. | |
| 15 | public boolean delete() |
| Deletes the file or directory represented by this abstract pathname. | |
| 16 | public void deleteOnExit() |
| Requests that the file or directory represented by this abstract pathname be deleted when the virtual machine terminates. | |
| 17 | public String[] list() |
| Returns a string array of the names of the files and directories in the directory represented by this abstract pathname. | |
| 18 | public String[] list(FilenameFilter filter) |
| Returns a string array consisting of the names of the files and directories contained in the directory, which is represented by the abstract pathname that satisfies the specified filter. | |
| 19 | public File[] listFiles() |
| Returns an array of abstract pathnames representing the files in the directory represented by this abstract pathname. | |
| 20 | public File[] listFiles(FileFilter filter) |
| Returns an array of abstract pathnames representing the files and directories in the directory represented by this abstract pathname, where the pathnames satisfy a specific filter. | |
| 21 | public boolean mkdir() |
| Creates the directory specified by this abstract pathname. | |
| 22 | public boolean mkdirs() |
| Creates the directory specified by this abstract pathname, including creating any necessary but non-existent parent directories. | |
| 23 | public boolean renameTo(File dest) |
| Renames the file represented by this abstract pathname. | |
| 24 | public boolean setLastModified(long time) |
| Sets the last modified time of the file or directory specified by this abstract pathname. | |
| 25 | public boolean setReadOnly() |
| Marks the file or directory specified by this abstract pathname so that it can only be read. | |
| 26 | public static File createTempFile(String prefix, String suffix, File directory) throws IOException |
| Creates a new empty file in the specified directory, generating its name using the given prefix and suffix strings. | |
| 27 | public static File createTempFile(String prefix, String suffix) throws IOException |
| Creates an empty file in the default temporary file directory, generating its name using the given prefix and suffix. | |
| 28 | public int compareTo(File pathname) |
| Compares two abstract pathnames in alphabetical order. | |
| 29 | public int compareTo(Object o) |
| Compares the abstract pathname with the given object in alphabetical order. | |
| 30 | public boolean equals(Object obj) |
| Tests whether this abstract pathname is equal to the given object. | |
| 31 | public String toString() |
| Returns the pathname string of this abstract pathname. |
Example
The following example demonstrates the use of a 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 compilation and execution results of the above example are 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