我需要使用groovy合并两个非常大(大于可用内存)的文件。
为了做到这一点,我提出了以下伪代码,我可以实现,除了来自FileA""读取lineA。
// Pseudo Code:
open fileA for Input
open fileB for Input
open fileOut for Output
read lineA from fileA
read lineB from fileB
notDone = true
while (notDone) {
if (lineA == lineB) {
write lineA to fileOut;
read lineA from fileA
read lineB from fileB
}
elseif (lineA > lineB) {
write lineB to fileOut;
read lineB from file B
}
else { // lineA < lineB
write lineA to fileOut;
read lineA from fileA
}
if (lineA == null || lineB == null) {
notDone= false
}
}
上面的伪代码使用&lt;,&gt;和==来比较线条,现实世界的例子更复杂。
答案 0 :(得分:1)
一位朋友帮我写了这个:
// ---------------------------------------------------------------------------------
BufferedReader bufferedReader_A = new BufferedReader(new FileReader("view-connections_6.txt"));
BufferedReader bufferedReader_B = new BufferedReader(new FileReader("view-connections_7.txt"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("OUTPUT.txt"));
// ---------------------------------------------------------------------------------
String lineA = bufferedReader_A.readLine()
String lineB = bufferedReader_B.readLine()
// ---------------------------------------------------------------------------------
Boolean notDone = true;
while(notDone) {
if (lineA==lineB) {
bufferedWriter.writeLine(lineA);
lineA = bufferedReader_A.readLine()
lineB = bufferedReader_B.readLine()
}
// ---------------------------------------------------------------------------------
else if (lineA > lineB) {
bufferedWriter.writeLine(lineB);
lineB = bufferedReader_B.readLine()
}
// ---------------------------------------------------------------------------------
else {
bufferedWriter.writeLine(lineA);
lineA = bufferedReader_A.readLine()
}
if (lineA == null || lineB == null) {
notDone=false;
}
}