How to check if a file exists at the specified path

In this blog post, I will be explaining how you can check if a file exists at a specified path. Consider the following code snippet:

public class FileExists {public static void main(String args[]){String fileName = "C:/Test.txt";File file = new File(fileName);if(file.exists()){System.out.println("File is present");}elseSystem.out.println("File is missing");}}

 

Here the File.exists method is used. This returns a true if a file with the specified name exists at the path specified, otherwise it returns a false.

If you run this code, you will get the following output (Assuming there is a file on C drive called test.txt):

File is present

Comments

Popular posts from this blog

How to use logging in SpringBoot with code samples

Python While Loop with code samples

How to convert a List to a Set