Explore Java Hacks and Tutorials
Be Social. Be Updated.

Using file.exists() to check the existence of a File in Java IO


Checking the existence of a file is easy in Java and is a method behind. The file.exists() method in java.io.File does the thing. Here is a sample tutorial.


import java.io.*;
class FileExistence
{
public static void main(String args[])
{
File f=new File("myfile.txt");
// Print the name
System.out.println("The name of the file is "+f.getName());
// Does the file exist
System.out.println("Does the file exist? "+f.exists());
}
}
f.exists(): Check the existence of a file. If the file is present, it returns true else it returns false.

-----------------------------------
Output
-----------------------------------
The name of the file is myfile.txt
Does the file exist? false

MPVWCWTNM4YU



Posted by at on

Tags: Java IO