• August 30, 2014
  • Core Java
  • Comments Off on How To Check If A File Exists In Java

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 javawithease
 */
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.

Related Topics:

Input output (I/O) in java.
FileInputStream and FileOutputStream in java with example.
Byte Streams in java with example.
DataInputStream and DataOutputStream in java with example.
BufferedInputStream and BufferedOutputStream in java with example.
FileReader and FileWriter in java with example.
How To Check If A File Exists In Java.
Serialization in java with example.
Transient in java with example.

 


Comments are closed.