意思是说这思路不行???
import
java.io.*;
import java.util.PriorityQueue;
public class ExternalSorter {
public static void main(String[] args) throws IOException {
String inputFile = "bigfile.txt"; // 输入文件路径
String tempDirectory = "temp"; // 临时文件目录
int maxMemory = 1024*1024*1024; // 假设最大内存使用量限制为 1GB
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile))) {
String line;
int fileCounter = 0;
while ((line = reader.readLine()) != null) {
// 使用 PriorityQueue 在内存中对行进行排序
PriorityQueue<String> sortedLines = new PriorityQueue<>();
sortedLines.add(line);
// 继续读取行,直到内存使用达到限制
int memoryUsage = line.length();
while (memoryUsage <= maxMemory && (line = reader.readLine()) != null) {
sortedLines.add(line);
memoryUsage += line.length();
}
// 将排序后的行写入临时文件
File tempFile = new File(tempDirectory, "tempfile" + fileCounter + ".txt");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) {
while (!sortedLines.isEmpty()) {
writer.write(sortedLines.poll());
writer.newLine();
}
}
fileCounter++;
}
}
// 合并临时文件
mergeTemporaryFiles(tempDirectory);
// 清理临时文件
cleanUpTemporaryFiles(tempDirectory);
}
private static void mergeTemporaryFiles(String tempDirectory) throws IOException {
File[] tempFiles = new File(tempDirectory).listFiles();
PriorityQueue<BufferedReader> readers = new PriorityQueue<>((br1, br2) -> {
try {
String line1 = br1.readLine();
String line2 = br2.readLine();
return line1.compareTo(line2);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
for (File tempFile : tempFiles) {
BufferedReader reader = new BufferedReader(new FileReader(tempFile));
readers.add(reader);
}
String outputFile = "output.txt"; // 输出文件路径
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
while (!readers.isEmpty()) {
BufferedReader reader = readers.poll();
String line = reader.readLine();
if (line != null) {
writer.write(line);
writer.newLine();
readers.add(reader);
} else {
reader.close();
}
}
}
}
private static void cleanUpTemporaryFiles(String tempDirectory) {
File[] tempFiles = new File(tempDirectory).listFiles();
for (File tempFile : tempFiles) {
tempFile.delete();
}
}
}