How to check if a file exists in Java

To determine whether a file is exist in your file system, use the Java IO File.exists().

package com.mkyong.io;
 
import java.io.*;
 
public class FileChecker {
 
  public static void main(String args[]) {
 
	  File f = new File("c:\\mkyong.txt");
 
	  if(f.exists()){
		  System.out.println("File existed");
	  }else{
		  System.out.println("File not found!");
	  }
 
  }
 
}
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments