Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Checking Whether a File Exists

Java Example - Checking Whether a File Exists

Java Examples Java Examples

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 Java Examples