Remove blank lines that appear when writing to file (Java)
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 i开发者_如何学编程nserted 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
Make a small change:
if (words[i]!=null && words[i].trim().length() != 0)
fw.println(words[i]);
Basically, you want to check and see if the line would be blank, and skip printing it.
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.
精彩评论