Java 如何在特定目录中列出所有新创建的文件?

Java 如何在特定目录中列出所有新创建的文件?,java,file,Java,File,列出在两个时间戳之间创建的文件的方法是什么?我想列出所有新创建的文件,然后将它们移动到其他目录 我正在Windows上工作如果您想主动监视新创建的文件,可以使用监视服务: 如果您只查找存在的文件,例如,您可以使用Apache Commons FileUtils类列出在两个特定日期之间修改/创建的所有文件。如果您确实想主动注意新创建的文件,可以使用WatchService: public void afterDate() throws IOException { final String

列出在两个时间戳之间创建的文件的方法是什么?我想列出所有新创建的文件,然后将它们移动到其他目录


我正在Windows上工作

如果您想主动监视新创建的文件,可以使用监视服务:


如果您只查找存在的文件,例如,您可以使用Apache Commons FileUtils类列出在两个特定日期之间修改/创建的所有文件。

如果您确实想主动注意新创建的文件,可以使用WatchService:

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());
    }

}
如果您只查找存在的文件,例如,可以使用Apache Commons FileUtils类列出在两个特定日期之间修改/创建的所有文件。

public void afterDate()引发IOException{
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());
    }

}
最后一个字符串pathToDirectory=“/path/to/directory”; final long afterDate=新日期().getTime(); 最终列表路径=新的ArrayList(); 最终路径目录=Path.get(pathToDirectory); try(DirectoryStream DirectoryStream=Files.newDirectoryStream(directory)){ for(路径:directoryStream){ final BasicFileAttributes attr=Files.readAttributes(路径,BasicFileAttributes.class); 最终长creationTime=attr.creationTime().toMillis(); 如果(creationTime>=afterDate){ 路径。添加(路径); } } } 用于(最终路径:路径){ System.out.println(path.getFileName()); } }
public void afterDate()引发IOException{
最后一个字符串pathToDirectory=“/path/to/directory”;
final long afterDate=新日期().getTime();
最终列表路径=新的ArrayList();
最终路径目录=Path.get(pathToDirectory);
try(DirectoryStream DirectoryStream=Files.newDirectoryStream(directory)){
for(路径:directoryStream){
final BasicFileAttributes attr=Files.readAttributes(路径,BasicFileAttributes.class);
最终长creationTime=attr.creationTime().toMillis();
如果(creationTime>=afterDate){
路径。添加(路径);
}
}
}
用于(最终路径:路径){
System.out.println(path.getFileName());
}
}

逻辑如下

使用文件类

遍历目录中的所有文件

可以使用file.listFiles()方法完成此操作

该方法将返回目录中的所有文件(文件和目录)

然后,对于每个文件对象,使用file.lastModified()获取时间戳,然后检查它是否在指定的时间戳之间


startTimestamp逻辑如下

使用文件类

遍历目录中的所有文件

可以使用file.listFiles()方法完成此操作

该方法将返回目录中的所有文件(文件和目录)

然后,对于每个文件对象,使用file.lastModified()获取时间戳,然后检查它是否在指定的时间戳之间


startTimestamp

您将传统文件API与NIO.2文件API混合使用。考虑用路径.file注释替换文件:本地文件系统不需要URI。您可以使用Paths.get(String,String…)方法。@Puce,再次感谢。。删除
URI
让我感觉更轻松,因为您将遗留文件API与NIO.2文件API混合在一起。考虑用路径.file注释替换文件:本地文件系统不需要URI。您可以使用Paths.get(String,String…)方法。@Puce,再次感谢。。删除
URI
让我感觉更自在