gpt4 book ai didi

java - 从文件夹 : largest, 检索最新的文件,无论日期如何

转载 作者:行者123 更新时间:2023-11-30 02:47:19 27 4
gpt4 key购买 nike

我想使用以下规则从文件夹中检索文件:

  1. 获取最大的文件
  2. 如果文件大小相同,则采用最新的文件。

到目前为止我已经尝试过以下方法:

List<Path> folderFilePaths = new ArrayList<Path>();

TreeMap<Date,List<Path>> filesByDay = new TreeMap<>();

for (Path filePath : folderFilePaths) {
String fileName = filePath.toFile().getName();
String[] fileNameParts = fileName.split("\\.");

filesByDay.add(filePath);
}

Path largestFile = filesByDay.get(0);
for (int i=1; i<filesByDay.size(); i++){
if (largestFile.toFile().length() < filesByDay.get(i).toFile().length()) {
largestFile = filesByDay.get(i);
}
}

最佳答案

    class Tuple<T1, T2, T3> {
public T1 first;
public T2 second;
public T3 third;

public Tuple(T1 first, T2 second, T3 third) {
this.first = first;
this.second = second;
this.third = third;
}
}


List<Tuple<File, Long, Long>> folderFiles = new ArrayList<Tuple<File, Long, Long>>();
for (Path filePath : folderFilePaths) {
File f = filePath.toFile();
folderFiles.add(new Tuple<>(f, f.length(), f.lastModified()));
}

Collections.sort(folderFiles, new Comparator<Tuple<File, Long, Long>>() {
@Override
public int compare(Tuple<File, Long, Long> lhs, Tuple<File, Long, Long> rhs) {
if (lhs.second == rhs.second)
if (lhs.third == rhs.third)
return 0;
else
return lhs.third > rhs.third ? -1 : 1;
else
return lhs.second > rhs.second ? -1 : 1;
}
});

folderFiles.get(0).first; // the top one is the largest, or newest

关于java - 从文件夹 : largest, 检索最新的文件,无论日期如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39794992/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com