![](/img/trans.png)
[英]Eclipse Project Directory does not show newly created files in the terminal
[英]How to list all newly created files in a specific directory?
如何列出例如在两个时间戳之间创建的文件的方式? 我想列出所有新创建的文件,然后将它们移到另一个目录。
我在Windows上工作
public void afterDate() throws IOException {
final String pathToDirectory = "/path/to/directory";
final long afterDate = new Date().getTime();
final List<Path> paths = new ArrayList<>();
final Path directory = Paths.get(pathToDirectory);
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
for (Path path : directoryStream) {
final BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
final long creationTime = attr.creationTime().toMillis();
if (creationTime >= afterDate) {
paths.add(path);
}
}
}
for (final Path path : paths) {
System.out.println(path.getFileName());
}
}
逻辑如下
使用文件类。
遍历目录中的所有文件
可以使用file.listFiles()方法完成。
该方法将返回目录中的所有文件(文件以及目录)。
然后对于每个文件对象,使用file.lastModified()获取时间戳,然后检查它是否在您指定的时间戳之间
startTimestamp <file.lastModified()<endTimestamp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.