How To Check If A File Exists In Java

Example:

FileCheckExample.java

import java.io.File;
 
/**
 * This program is used to check that file 
 * exists or not at given location. 
 * @author w3spoint
 */
class IOTest{
	public void checkFileExistance(){
		//Creating File object.
		File file = 
                     new File("F:\\New folder\\data1.txt");
		if(file.exists()){
			System.out.println("file exist.");
		}else{
			System.out.println("file not exist.");
		}
	}
}
 
public class FileCheckExample {
	public static void main(String args[]){
		//Creating IOTest object.
		IOTest obj = new IOTest();
 
		//method call.
		obj.checkFileExistance();
	}
}

Output:

file exist.

Download this example.
 
Next Topic: Serialization in java with example.
Previous Topic: FileReader and FileWriter in java with example.

 

Content Protection by DMCA.com
Please Share