java - 项目开始运行时如何检查文件是否存在?
问题描述
我是java编程新手,所以也许我的问题对你们中的一些人来说似乎很愚蠢。
我将 netbeans 用于我的 java web 项目。每当触发项目时,我都需要检查文件系统中是否存在某些文件。
所以,我想知道当项目启动时,我可以在哪里放置检查文件是否存在的功能(即项目启动项目在哪里)?
解决方案
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
推荐阅读
- android - Swift 4 等效于 Kotlin“匿名类”表示法
- swift - 在静态框架内链接动态库
- java - 如何验证游标是否在数据库中关闭?
- project-reactor - 如何测试在订阅之前什么都没有发生
- html - 文本装饰没有被删除(下划线被删除,但文本保持装饰)
- angular - 如何以编程方式跳过量角器测试?
- node.js - 使用猫鼬在快递中更改日期格式
- image-processing - 图像分类 - 有没有车
- python - gnuradio-companion ImportError:没有名为 main 的模块
- android - 错误:AAPT2 错误:查看日志了解详细信息