开发者

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 t开发者_开发技巧hat 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...


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜