Newline for BufferedWriter?

Dear Experts,

To generate a simple text file report, I'm using:
  FileWriter outFile = new FileWriter(outFileName, false);
  BufferedWriter outBufnew BufferedWriter(outFile);

And then to write I line, I do:
  outBuf.write(s + "\n");

However, when I open the file in NotePad, it doesn't recognize the eol character ("\n").  Is there a simple solution for this?  Is there a better choice than BufferedWriter to generate a simple text file report?

Thanks!
BrianMc1958
BrianMc1958Asked:
Who is Participating?
 
CEHJCommented:
Do instead

PrintWriter outFile = new PrintWriter(new FileWriter(outFileName, false));

outFile.println("xx");

Your newlines will then be platform-correct
0
 
objectsCommented:
>   outBuf.write(s + "\n");

should be

  outBuf.write(s + "\r\n");

No need to use PrintWriter (in fact u should avoid it in your case)
0
 
CEHJCommented:
>>
should be

  outBuf.write(s + "\r\n");
>>

Wrong - that will generate platform-specific code, which defeats one of the objects of using Java in the first  place
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
objectsCommented:
It will work fine on *all* platforms, try it and see :)
0
 
CEHJCommented:
What do you mean by 'work'? You can write anything you want to a file but it doesn't mean you should. I'm not sure why you'd advocate producing text files that are only idiomatically correct for Windows when firstly it's unnecessary and secondly it's incorrect.
0
 
objectsCommented:
Post was for BrianMc1958 :)  Let me know if you have any problems with it or questions.
0
 
BrianMc1958Author Commented:
Yikes.  Seemed like a simple question!

I'm the newbie, but I'm inclined to agree with CEHJ.  The instantiation of the PrintWriter looks as simple as the instantiation of the BufferedWriter, and then eliminates the ugly special characters.

To objects:  could you say anything more about why I shouldn't use PrintWriter?  Based on what I'm seeing, it looks like BufferedWriter is intended for plain vanilla data files, which don't necessarily contain end-of-line markers.  And PrintWriter (I'm guessing) is designed to do that specifically.  Is that right?
0
 
shmertCommented:
Yeh, listen to CEHJ, it's the right solution.  No reason not to use PrintWriter, unless you're generating files on one platform that you want to use on another platform.
0
 
CEHJCommented:
>>
Based on what I'm seeing, it looks like BufferedWriter is intended for plain vanilla data files, which don't necessarily contain end-of-line markers.  And PrintWriter (I'm guessing) is designed to do that specifically.  Is that right?
>>

Absolutely right
0
 
BrianMc1958Author Commented:
OK.  I'm sure I'll use both solutions at different times.  Thanks again, folks.
0
 
CEHJCommented:
:-)

>> I'm sure I'll use both solutions at different times.

I hope not ;-)
0
 
objectsCommented:
> could you say anything more about why I shouldn't use PrintWriter?

Try running your reports on a different platfiorm :)
you'll get the same problem you currently have when you try to view your reports in Notepad.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.