Java Example - Checking Whether a File Exists
The following example demonstrates how to use the File class's file.exists() method to check whether a file exists:
Main.java File
import
java
.
io
.
File
;
public
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
File
file
=
new
File
(
"
C:/java.txt
"
)
;
System
.
out
.
println
(
file
.
exists
(
)
)
;
}
}
The output of the above code is (if a java.txt file exists on your C: drive):
true
Java Examples