Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Newline for BufferedWriter?

Posted on 2007-10-15
12
Medium Priority
?
1,180 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:BrianMc1958
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1600 total points
ID: 20081515
Do instead

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

outFile.println("xx");

Your newlines will then be platform-correct
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 20082078
>   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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20083666
>>
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Expert Comment

by:objects
ID: 20083675
It will work fine on *all* platforms, try it and see :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20083688
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
 
LVL 92

Expert Comment

by:objects
ID: 20083700
Post was for BrianMc1958 :)  Let me know if you have any problems with it or questions.
0
 

Author Comment

by:BrianMc1958
ID: 20085081
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
 
LVL 11

Expert Comment

by:shmert
ID: 20086301
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20086497
>>
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
 

Author Comment

by:BrianMc1958
ID: 20088248
OK.  I'm sure I'll use both solutions at different times.  Thanks again, folks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20088477
:-)

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

I hope not ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 20089346
> 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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question