首页 > 解决方案 > 项目开始运行时如何检查文件是否存在?

问题描述

我是java编程新手,所以也许我的问题对你们中的一些人来说似乎很愚蠢。

我将 netbeans 用于我的 java web 项目。每当触发项目时,我都需要检查文件系统中是否存在某些文件。

所以,我想知道当项目启动时,我可以在哪里放置检查文件是否存在的功能(即项目启动项目在哪里)?

标签: java

解决方案


Java1 或更高版本

File file = new File("c:/foo.txt");
file.isFile();      // true - file is regular file (not a directory or smth. else)
file.exists();      // true - file exists
file.canRead();     // true - file exists and readable
file.canWrite();    // true - file exists and writable

Java7 或更高版本

Path file = Paths.get("c:/foo.txt");
Files.isRegularFile(file);    // true - file is regular file (not a directory or smth. else)
Files.exists(file);           // true - file exists
Files.isReadable(file);       // true - file exists and readable
Files.isWritable(file);       // true - file exists and writable

推荐阅读