Viji
Viji

Reputation:

Check if a text file exists

I need to pick a text file from a folder and read the data in the file. The file is generated dynamically in that folder in the format "XXX_2010-12-06". So, first i need to check if the file is existing in the folder and if it exists the content of the file should be read.

I have the code to read the content in the text file. I need to provide the path of the file

Can anyone help me in coding this using java...

Upvotes: 0

Views: 1669

Answers (2)

user238033
user238033

Reputation:

You can create a new instance of File and initiate it with the path to the file itself.

File file = new File("/a/path/to/a/file/TheFile.txt");

Once you have your File instance created you can check to see if it exists by calling the exists() method inside of File.

System.out.println(file.exists() ? "The file exists!" : "The file doesn't exist!");

I couldn't really understand what you were asking for help with. But if you edit your question to be more clear I will edit my answer to fulfill further answering.

Upvotes: 3

RedGrittyBrick
RedGrittyBrick

Reputation: 4002

The File class has methods for checking whether a file exists.

Upvotes: 1

Related Questions