-2

I'm using a buffered writer to write words in a text file, which works. But the writer starts at a new line every time, so you get all the words underneath each other with output.nextLine(); Can I check if a line is empty with an if statement so it wont print an empty line?

Here's the code:

BufferedWriter output = new BufferedWriter(new FileWriter("products.txt", true));
                    output.newLine();
                    output.append(s+" : "+price+" Euro");
                    output.close();

Because right now my txt file has an empty line at the top if I dont have text there.

  • You should edit your title to remove references to a buffered *reader*. It's also not very clear what you are asking. – Duncan Jones Mar 11 '15 at 09:55
  • Your question is really unclear. Can you explain in more detail what your problem is? Perhaps some examples would help. – Duncan Jones Mar 11 '15 at 10:00
  • Warkst answered it, cant update it yet. Thanks for your help! :) –  Mar 11 '15 at 10:02

1 Answers1

1

To write to a file line by line without printing empty lines, change your code to the following:

BufferedWriter output = new BufferedWriter(new FileWriter("products.txt", true));
output.append(s+" : "+price+" Euro\n");
output.close();
RDM
  • 4,986
  • 4
  • 34
  • 43
  • @Bas I recommend you create your `BufferedWriter` as specified in [this answer](http://stackoverflow.com/a/6998929/474189) so that you can specify a charset. – Duncan Jones Mar 11 '15 at 10:08