Tutorials / Tech blog / Geek Zone

Latest Posts

JAVA :: Check if a File exists

Sometimes, a need might arise to read files from a predefined location. It is always a best practice to check if the file exists. This can be achieved using Java IO Package --> File.exists()



Sample JAVA Project

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class FileCheck {

 public static void main(String[] args) {

  String filePath = "documents/test_file.txt";

  File file = new File(filePath);

  if (file.exists()) {
   System.out.println("File found...");
  } else {
   System.out.println("File not found...");
  }
 }
}

Project Structure


No comments:

Post a Comment