Check if file exists without creating it

后端 未结 4 1933
花落未央
花落未央 2020-12-03 05:05

If I do this:

File f = new File(\"c:\\\\text.txt\");

if (f.exists()) {
    System.out.println(\"File exists\");
} else {
    System.out.println(\"File not f         


        
4条回答
  •  孤城傲影
    2020-12-03 05:29

    The Files.exists method has noticeably poor performance in JDK 8, and can slow an application significantly when used to check files that don't actually exist.

    This can be applied too for Files.noExists, Files.isDirectory and Files.isRegularFile

    According this you can use the following :

    Paths.get("file_path").toFile().exists()
    

提交回复
热议问题