处理写入文件,因此使用FileWriter来回答,这显然不适用于此。 如果可能的话,我宁愿一个真正的API调用返回true / false到一些调用API来打开一个文件,并抓住它时,它会引发一个异常,你检查文本中没有文件,但我可以住在后者。 java.io.File : File f = new File(filePathString); if(f.exists()&&!f.isDirectory()){ //做某事} How can I check whether a file exists, before opening it for reading in Java? (equivalent of Perl's -e $filename).The only similar question on SO deals with writing the file and was thus answered using FileWriter which is obviously not applicable here.If possible I'd prefer a real API call returning true/false as opposed to some "Call API to open a file and catch when it throws an exception which you check for 'no file' in text", but I can live with the latter. 解决方案 Using java.io.File:File f = new File(filePathString);if(f.exists() && !f.isDirectory()) { // do something} 这篇关于如何检查Java中是否存在文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 11:51
查看更多