【发布时间】:2017-10-25 08:46:53
【问题描述】:
所以,我对编程和这个网站还是很陌生。 我一直在尝试制作自己的浏览器只是为了好玩,我正在学习如何编写和读取文本文件以用作日志来保存数据。我正在尝试制作一个收藏夹系统,以便我可以将不同的网站保存到日志中,但我不明白为什么我的部分代码不起作用。我在网上花了大约两个半小时环顾四周,没有发现任何问题。
try(BufferedReader format1 = new BufferedReader(new
FileReader("favorites.txt"))) {
String line = format1.readLine(); //Used to look through file
String found = ""; //Stores the address that was the same
//Loop searches file, I only plan to have a max of 20 favorites
for (int counter = 0; counter < 20; counter++) {
System.out.println(line); //Just used so I can make sure the
loop works
if(line.equals(input)) {
counter = 20;
found = line;
}
line = format1.readLine();
}
format1.close();
if(!(found.equals(input))) {
BufferedWriter format2 = new BufferedWriter(new
FileWriter("favorites.txt", true));
format2.write(input);
format2.newLine();
format2.close();
}
} catch (IOException e1) {
System.out.println("ERROR! Favorite not Added.");
}
我检查了问题所在,实际写入文件的 if 语句似乎是问题所在。我已经看了一百万次,我什么也看不到。也许更有经验的程序员可以看到我的问题?每当我运行这部分代码时,它都会吐出大量错误消息。感谢您的帮助!
【问题讨论】:
-
“它会吐出大量错误消息”。错误消息说了什么?你应该把这些读给understand what the problem is。如果在阅读了该帖子并检查了堆栈跟踪之后您仍然无法找出问题所在,请在问题中发布完整的堆栈跟踪并显示代码中的哪一行引发了异常。
-
@James_D 我试过了,但由于我比较新,我不太清楚他们的意思。很多与应用程序中的异常有关。奇怪的是,当我注释掉 for 循环时,它工作正常。不知道发生了什么。
-
这就是为什么我链接了解释如何读取堆栈跟踪的帖子。