How do I check if a file exists in Java?

后端 未结 17 2867
盖世英雄少女心
盖世英雄少女心 2020-11-22 00:50

How can I check whether a file exists, before opening it for reading in Java (the equivalent of Perl\'s -e $filename

17条回答
  •  清歌不尽
    2020-11-22 01:22

    For me a combination of the accepted answer by Sean A.O. Harney and the resulting comment by Cort3z seems to be the best solution.

    Used the following snippet:

    File f = new File(filePathString);
    if(f.exists() && f.isFile()) {
        //do something ...
    }
    

    Hope this could help someone.

提交回复
热议问题