按行合并两个文档
Merge two documents linewise
我正在尝试将两个文档 doc_a 和 doc_b 合并到第三个文档 doc_c 中,期望的结果是:
content doc_a: line1
line2
content doc_b: line3
line4
content doc_c: line1 line3
line2 line4
我的做法与此类似:
public static void mergeTrain(File src1, File src2, File dest) throws IOException{
Files.copy(src1.toPath(), dest.toPath());
BufferedReader br = new BufferedReader(new FileReader(src2));
BufferedWriter bw = new BufferedWriter(new FileWriter(dest,true));
String line = new String("Platzhalter");
try {
while(line != null){
line = br.readLine();
if (line !=null){
bw.append(line);
}
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
br.close();
bw.close();
}
结果我选择了:
content doc_c: line1
line2 line3 line4
如何获得我想要的结果?
您当前正在将 src1
的内容复制到目标文件 dest
,然后您附加文件 src2
中的所有行 - 这正是您得到的结果。你如何做你实际解释过的你想做的事?
这个怎么样:
public static void mergeTrain(File src1, File src2, File dest) throws IOException {
BufferedReader br1 = new BufferedReader(new FileReader(src1));
BufferedReader br2 = new BufferedReader(new FileReader(src2));
BufferedWriter bw = new BufferedWriter(new FileWriter(dest,true));
String line1;
String line2;
try {
while((line1 = br1.readLine()) != null
&& (line2 = br2.readLine()) != null) {
bw.append(line1 + " " + line2);
bw.newLine();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
br1.close();
br2.close();
bw.close();
}
我没有测试代码,但你至少应该明白了。
请注意,如果两个文件的行数不同,您将需要考虑要做什么。
最好将两个输入文件一起解析。另请注意,这两个文件的长度可能不同。
public static void mergeTrain(File src1, File src2, File dest) throws IOException{
BufferedReader br1 = new BufferedReader(new FileReader(src1));
BufferedReader br2 = new BufferedReader(new FileReader(src2));
BufferedWriter bw = new BufferedWriter(new FileWriter(dest,true));
String line1 = "";
String line2 = "";
try {
while(line1 != null || line2!=null){
line1 = line1 !=null && br1.readLine();
line2 = line2 != null && br2.readLine();
if (line1 !=null) && (line2!=null){
bw.append(line1).append(" ").append(line2).append("\n");
}
} else if (line1!=null) {
bw.append(line1).append("\n");
} else if (line2!=null) {
bw.append(line2).append("\n");
}
}
br1.close();
br2.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我正在尝试将两个文档 doc_a 和 doc_b 合并到第三个文档 doc_c 中,期望的结果是:
content doc_a: line1
line2
content doc_b: line3
line4
content doc_c: line1 line3
line2 line4
我的做法与此类似:
public static void mergeTrain(File src1, File src2, File dest) throws IOException{
Files.copy(src1.toPath(), dest.toPath());
BufferedReader br = new BufferedReader(new FileReader(src2));
BufferedWriter bw = new BufferedWriter(new FileWriter(dest,true));
String line = new String("Platzhalter");
try {
while(line != null){
line = br.readLine();
if (line !=null){
bw.append(line);
}
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
br.close();
bw.close();
}
结果我选择了:
content doc_c: line1
line2 line3 line4
如何获得我想要的结果?
您当前正在将 src1
的内容复制到目标文件 dest
,然后您附加文件 src2
中的所有行 - 这正是您得到的结果。你如何做你实际解释过的你想做的事?
这个怎么样:
public static void mergeTrain(File src1, File src2, File dest) throws IOException {
BufferedReader br1 = new BufferedReader(new FileReader(src1));
BufferedReader br2 = new BufferedReader(new FileReader(src2));
BufferedWriter bw = new BufferedWriter(new FileWriter(dest,true));
String line1;
String line2;
try {
while((line1 = br1.readLine()) != null
&& (line2 = br2.readLine()) != null) {
bw.append(line1 + " " + line2);
bw.newLine();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
br1.close();
br2.close();
bw.close();
}
我没有测试代码,但你至少应该明白了。 请注意,如果两个文件的行数不同,您将需要考虑要做什么。
最好将两个输入文件一起解析。另请注意,这两个文件的长度可能不同。
public static void mergeTrain(File src1, File src2, File dest) throws IOException{
BufferedReader br1 = new BufferedReader(new FileReader(src1));
BufferedReader br2 = new BufferedReader(new FileReader(src2));
BufferedWriter bw = new BufferedWriter(new FileWriter(dest,true));
String line1 = "";
String line2 = "";
try {
while(line1 != null || line2!=null){
line1 = line1 !=null && br1.readLine();
line2 = line2 != null && br2.readLine();
if (line1 !=null) && (line2!=null){
bw.append(line1).append(" ").append(line2).append("\n");
}
} else if (line1!=null) {
bw.append(line1).append("\n");
} else if (line2!=null) {
bw.append(line2).append("\n");
}
}
br1.close();
br2.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}