java

How to check if a file exists in Java

In this tutorial, we are going to see how to check if a file exists or not in Java. To check if a file exists in your file system, simply use Java IO File.exists().
 

How to check if a file exists in Java
import java.io.*;

public class Main {
	
  public static void main(String args[]) {
    
	  File file = new File("C:\\Users\\PC\\Desktop\\test.txt"); 

	  if(file.exists()){
		  System.out.println("File exists.");
	  }else{
		  System.out.println("File doesn't exists.");
	  }
  }
}

Output:

File exists.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *