【发布时间】:2016-08-19 17:09:48
【问题描述】:
我正在尝试识别文件每一行中字符 (=) 之后的单词并将其保存到另一个文件中。以下是我到目前为止编写的代码,但它无法正常工作。任何想法编码器?
My input.txt file is :
President=obama
Vice President=Biden
Head of State=Kerry
package Test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.StringJoiner;
public class Stringbuilder {
public static void main(String[] args) throws FileNotFoundException, IOException {
{
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
String[] results = sCurrentLine.split("");
String word = sCurrentLine;
String Sym = "=";
for (int i = 0; i < sCurrentLine.length(); i++) {
if (word.contains(Sym)) {
// System.out.println(results[1]);
for (int j = 0; j < sCurrentLine.length(); j++) {
String content = results[j];
System.out.println(content);
File file = new File("output.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Done");
}
}
}
}
}
}
}
}
【问题讨论】:
-
it is not working correctly,需要详细说明吗?提供输入/输出/堆栈跟踪。由于缺乏细节投票结束。 -
请添加示例输入文件和所需的输出。
-
我已经添加了输入文件的详细信息。
-
克里是国务卿,不是国家元首,这点很不一样