我正在尝试编写类似文本文件解释器的东西。我决定从基础开始,写一些剖析字符串的东西:
public static void dataImport() throws IOException {
BufferedReader dataReader = new BufferedReader(new FileReader("C:\\Users\\(my name)\\Desktop\\javaWrite.txt"));
ArrayList<String> orderLine = new ArrayList<>();
String line = null;
String endResult = "";
String currentChar = null;
while((line = dataReader.readLine()) != null) {
for(int character = 0; character < line.length(); character++) {
currentChar = line.substring(character, character+1);
if(currentChar.equals("|")) {
for(String singleChar : orderLine) {
endResult += singleChar;
}
fileThings.add(endResult);
endResult = "";
orderLine.clear();
}else{
orderLine.add(currentChar);
}
}
}
dataReader.close();
}
它正在读取的文本文件有“Hello|world!”写。
代码输出“Hello”,没有别的。当我让它在 else 中输出 currentChar 变量时,它只是继续阅读整个句子。所以所有证据都表明 ArrayList orderLine 是问题所在。
最佳答案
您的代码仅在看到 | 时才将数据附加到数组列表中输入数据中的字符。您的输入数据仅包含一个 |特点。因此,您在输出 ArrayList 中只会得到一行。
关于Java 突然停止写入 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34644344/