解决文件排序问题

master
zhanghai 2 years ago
parent 54d01935f2
commit d2ae98ceab

@ -12,10 +12,7 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
@Slf4j
public class FenpanService {
@ -95,8 +92,8 @@ public class FenpanService {
public Map<String, Zong> readFile(String readUrl) {
//读取文件夹。
List<Pieces> pieces = new ArrayList<>();
Map<String, Roll> rollMap = new HashMap<>();
Map<String, Zong> zongMap = new HashMap<>();
Map<String, Roll> rollMap = new LinkedHashMap<>();
Map<String, Zong> zongMap = new LinkedHashMap<>();
this.findAllDir(readUrl, pieces);
//写入文件
@ -152,21 +149,59 @@ public class FenpanService {
}
roll.putPieces(piece);
rollMap.put(name, roll);
}
//填充宗
for (String name : rollMap.keySet()) {
Roll roll = rollMap.get(name);
File file = new File(roll.getAbsolutePath());
String zongName = file.getParentFile().getName();
String zongName = parentFile.getParentFile().getName();
Zong zong = zongMap.get(zongName);
if (zong == null) {
zong = new Zong(file.getParentFile().getAbsolutePath(), file.getParentFile().getName());
zong = new Zong(parentFile.getParentFile().getAbsolutePath(), parentFile.getParentFile().getName());
}
zong.put(rollMap.get(name));
// zong.put(rollMap.get(name));
zongMap.put(zongName, zong);
}
for(String zongName: zongMap.keySet()){
Zong zong = zongMap.get(zongName);
File zongfile = new File(zong.getAbsolutePath());
File[] files = zongfile.listFiles();
// 创建自定义比较器
Comparator<File> fileComparator = new Comparator<File>() {
@Override
public int compare(File file1, File file2) {
// 使用正则表达式提取数字部分
int num1 = extractNumber(file1.getName());
int num2 = extractNumber(file2.getName());
// 比较提取的数字部分
return Integer.compare(num1, num2);
}
private int extractNumber(String fileName) {
String numberPart = fileName.replaceAll("[^0-9]", "");
return numberPart.isEmpty() ? 0 : Integer.parseInt(numberPart);
}
};
Arrays.sort(files, fileComparator);
for(File file:files){
Roll roll = rollMap.get(file.getName());
zong.put(roll);
}
}
// //填充宗
// for (String name : rollMap.keySet()) {
// Roll roll = rollMap.get(name);
// File file = new File(roll.getAbsolutePath());
// String zongName = file.getParentFile().getName();
// Zong zong = zongMap.get(zongName);
// if (zong == null) {
// zong = new Zong(file.getParentFile().getAbsolutePath(), file.getParentFile().getName());
// }
// zong.put(rollMap.get(name));
// zongMap.put(zongName, zong);
// }
return zongMap;

Loading…
Cancel
Save