我想复制文件1中每一行的最后一个单词,并将其放在文件2中的每一行。
文件1
x n o n
y n n o
z n n n
文件2
n, n, n, B-S
n, o, o, I-S
n, n, n, S
输出
x n o n B-S
y n n o I-S
z n n n S
我的代码
File f = new File("C:\\file.txt");
BufferedReader reader = new BufferedReader(new FileReader(f));
File f1 = new File("C:\\file1.txt");
BufferedReader reader1 = new BufferedReader(new FileReader(f1));
String line = null;
String line1 = null;
while ((line1 = reader1.readLine())!= null) {
String c = line1.substring(line1.lastIndexOf(" ")+1);
while((line = reader.readLine()) != null) {
FileWriter fileWritter = new FileWriter(f.getName(),true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(c);
bufferWritter.close();
}
}
答案 0 :(得分:0)
由于您似乎知道如何读写文件,因此我为您提供了一个简单的代码来获取字符串中每一行的最后一个单词。这里只是基本拆分:
String input1 = "x n o n\ny n n o\nz n n n";
String input2 = "n, n, n, B-S\nn, o, o, I-S\nn, n, n, S";
String[] input1Lines = input1.split("\n");
String[] input2Lines = input2.split("\n");
String result = "";
for(int i=0; i<input2Lines.length; i++) {
String[] words = input2Lines[i].split(", ");
String lastWord = words[words.length-1];
result += input1Lines[i] + " " + lastWord + "\n";
}
System.out.println(result);