删除写入文件时出现的空行(Java)
我创建了一个程序,它接受一个充满 3 个字母单词的文本文件并处理它们,将它们存储在一个数组中,然后输出到 JCreator 中的构建输出,然后将相同的输出写入文件。
现在,这个程序工作正常,但是当我打印大量数据时 - 我将所有这些空白行插入到不应该有的地方。
我用它来打印到我的文件:
PrintWriter fw = new PrintWriter(new FileWriter("Dictionary.txt"));
for (int i=0; i<count; i++)
{
if (words[i]!=null)
fw.println(words[i]);
}
我循环遍历数组,但不打印到文件,只是打印到 IDE 上的输出屏幕。现在,我会突然在不应该有的地方出现一个空行,如下所示:
啧啧
晚礼服乌克
使用
它似乎是完全随机的。
现在,我如何从文件中删除这些行,而不必写入新文件,因为写入大量行似乎会导致此问题。
谢谢大家
I have created a program which takes a text file full of 3 letter words and processes them, stores them in an array and then outputs to the build output in JCreator and then writes the same output to a file.
Now, this program works fine, but when I print a lot of data - I get all of these blank lines inserted where there shouldn't be any.
I use this to print to my file:
PrintWriter fw = new PrintWriter(new FileWriter("Dictionary.txt"));
for (int i=0; i<count; i++)
{
if (words[i]!=null)
fw.println(words[i]);
}
I loop through the array and don't print to a file, just to my output screen on the IDE. Now, I will suddenly get a blank line where there shouldn't be, like so:
tut
tuxuke
use
and it seems to be completely random.
Now how would I remove these lines from the file without having to write to a new file, as writing large amounts of lines seems to cause this problem.
Thanks guys
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
进行一个小更改:
基本上,您想要检查并查看该行是否为空,并跳过打印它。
Make a small change:
Basically, you want to check and see if the line would be blank, and skip printing it.
如果它是完全随机的,那么您应该尝试写入一个文件并检查该文件。并不是说我不信任你们 IDE 提供的控制台。
If it's totally random, then you should try to write in a file and check this file. Not that I don't trust the console provided by your IDE.