Java 按上次编辑的时间读取文件夹中的所有.txt文件

Java 按上次编辑的时间读取文件夹中的所有.txt文件,java,file,Java,File,我这里有一个代码,可以读取1个文件夹中的所有.txt文件,它可以将.txt文件中的所有内容打印到控制台。然后它移动到新文件夹 问题是:它已被随机读取,但我想通过它的时间戳读取.txt文件,这是谁上次编辑的时间将首先读取 import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.FileSystems; import java.nio.file.Files; import java.

我这里有一个代码,可以读取1个文件夹中的所有.txt文件,它可以将.txt文件中的所有内容打印到控制台。然后它移动到新文件夹

问题是:它已被随机读取,但我想通过它的时间戳读取.txt文件,这是谁上次编辑的时间将首先读取

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Basic {

    public static void main(String[] args) throws IOException {
        String source = "C:\\Users\\NN\\Documents\\Test1";
        String target = "C:\\Users\\NN\\Documents\\Test2";

        List<Path> filePaths = filePathsList(source); // Step 1: get all files from a directory
        List<Path> filteredFilePaths = filter(filePaths); // Step 2: filter by ".txt"
        Map<Path, List<String>> contentOfFiles = getContentOfFiles(filteredFilePaths); // Step 3: get content of files
        move(filteredFilePaths, target); // Step 4: move files to destination
        printToConsole(contentOfFiles); // Step 5: print content of files to console
    }

    public static List<Path> filePathsList(String directory) throws IOException {
        List<Path> filePaths = new ArrayList<>();
        DirectoryStream<Path> directoryStream = Files.newDirectoryStream(FileSystems.getDefault().getPath(directory));
        for (Path path : directoryStream) {
            filePaths.add(path);
        }
        return filePaths;
    }

    private static List<Path> filter(List<Path> filePaths) {
        List<Path> filteredFilePaths = new ArrayList<>();
        for (Path filePath : filePaths) {
            if (filePath.getFileName().toString().endsWith(".txt")) {
                filteredFilePaths.add(filePath);
            }
        }
        return filteredFilePaths;
    }

    private static Map<Path, List<String>> getContentOfFiles(List<Path> filePaths) throws IOException {
        Map<Path, List<String>> contentOfFiles = new HashMap<>();
        for (Path filePath : filePaths) {
            contentOfFiles.put(filePath, new ArrayList<>());
            Files.readAllLines(filePath).forEach(contentOfFiles.get(filePath)::add);
        }
        return contentOfFiles;
    }

    private static void move(List<Path> filePaths, String target) throws IOException {
        Path targetDir = FileSystems.getDefault().getPath(target);
        if (!Files.isDirectory(targetDir)) {
            targetDir = Files.createDirectories(Paths.get(target));
        }
        for (Path filePath : filePaths) {
            System.out.println("Moving " + filePath.getFileName() + " to " + targetDir.toAbsolutePath());
            Files.move(filePath, Paths.get(target, filePath.getFileName().toString()), StandardCopyOption.ATOMIC_MOVE);
        }   
    }

    private static void printToConsole(Map<Path, List<String>> contentOfFiles) {
        System.out.println("Content of files:");
        contentOfFiles.forEach((k,v) -> v.forEach(System.out::println));
    }
}
import java.io.IOException;
导入java.nio.file.DirectoryStream;
导入java.nio.file.FileSystems;
导入java.nio.file.Files;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.nio.file.StandardCopyOption;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
公共课基础{
公共静态void main(字符串[]args)引发IOException{
String source=“C:\\Users\\NN\\Documents\\Test1”;
String target=“C:\\Users\\NN\\Documents\\Test2”;
List filepath=filePathsList(source);//步骤1:从目录获取所有文件
List filteredFilePaths=filter(filepath);//步骤2:按“.txt”筛选
Map contentOfFiles=getContentOfFiles(FilteredFilePath);//步骤3:获取文件的内容
移动(FilteredFilePath,目标);//步骤4:将文件移动到目标
printToConsole(contentOfFiles);//步骤5:将文件内容打印到控制台
}
公共静态列表文件路径列表(字符串目录)引发IOException{
List filepath=new ArrayList();
DirectoryStream DirectoryStream=Files.newDirectoryStream(FileSystems.getDefault().getPath(directory));
for(路径:directoryStream){
添加(路径);
}
返回文件路径;
}
专用静态列表筛选器(列表文件路径){
List FilteredFilePath=new ArrayList();
用于(路径文件路径:文件路径){
if(filePath.getFileName().toString().endsWith(“.txt”)){
添加(文件路径);
}
}
返回过滤器路径;
}
私有静态映射getContentOfFiles(列表文件路径)引发IOException{
Map contentOfFiles=new HashMap();
用于(路径文件路径:文件路径){
put(filePath,new ArrayList());
Files.readAllLines(filePath).forEach(contentOfFiles.get(filePath)::添加);
}
返回contentOfFiles;
}
私有静态无效移动(列表文件路径、字符串目标)引发IOException{
Path targetDir=FileSystems.getDefault().getPath(目标);
如果(!Files.isDirectory(targetDir)){
targetDir=Files.createDirectories(path.get(target));
}
用于(路径文件路径:文件路径){
System.out.println(“将“+filePath.getFileName()+”移动到“+targetDir.toabsolutionPath());
Files.move(filePath,Paths.get(target,filePath.getFileName().toString()),StandardCopyOption.ATOMIC_move);
}   
}
私有静态void printToConsole(映射contentOfFiles){
System.out.println(“文件内容:”);
contentOfFiles.forEach((k,v)->v.forEach(System.out::println));
}
}

您可以使用
File.lastModified()
并按日期对其进行排序。

我必须创建一个新类,或者在筛选.txt文件时应该编辑第二步?