This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes About PrintWriter with println and write Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "About PrintWriter with println and write" Watch "About PrintWriter with println and write" New topic
Author

About PrintWriter with println and write

Karim Kiswarday
Ranch Hand

Joined: Oct 01, 2010
Posts: 50

Hello friends,
I'm studying the Java I/O but I have a little bit of confusing, can you help me?

I will do examples only with the character stream, because (I think) the byte stream is the same.
I have studied the I/O hierarchy and I know I can chain two or more classes to improve the funcionality, but I don't understand when (and why) I need to use the PrintWriter class (and the PrintStream also)

I have written this code:
1) In the first block I use only the FileWriter with the write method
2) In the second PrintWriter with write
3) PrintWriter with println
4) PrintWriter+FileWriter with write
5) PrintWriter+FileWriter with println
6) PrintWriter+FileOutputStream with println

I have also tried to write a float (a primitive) in my file, but the results are the same both with println and with write.

Somebody can explain me why I need to add PrintWriter in my chain?

Thanks,
KK


No fun at the table, no fun in the sack.
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495


when (and why) I need to use the PrintWriter class (and the PrintStream also)

I generally use PrintWriter for Java networking. Here is an example :



PrintStream prints to an OutputStream, and PrintWriter prints to a Writer... [Difference between] OutputStream and a Writer? Both are streams, with the primary difference being a OutputStream is a stream of bytes, while, a Writer it a stream of characters.

If an OutputStream deals with bytes, what about PrintStream.print(String)? It converts chars to bytes using the default platform encoding. Using the default encoding is generally a bad thing since it can lead to bugs when moving from one platform to another, especially if you are generating the file on one platform and consuming it on another.

Quote taken from : http://stackoverflow.com/questions/2822005/java-difference-between-printstream-and-printwriter

Hopefully this answers one portion of your question,
cc11rocks

EDIT (from the same quoted page...) :

With the PrintStream you're sticked to platform's default encoding.

With the PrintWriter you can however pass an OutputStreamWriter with a specific encoding.

The advantage is, well, that you can control the character encoding the characters should be written in so that they won't eventually end up as mojibake.


“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
Karim Kiswarday
Ranch Hand

Joined: Oct 01, 2010
Posts: 50

Thanks for your reply, I have understood why I need to use PrintWriter now.
But I don't know how to arrange PrintWriter with the other classes.

In the example 1, I can write on a file without PrintWriter (I use FileWriter)
In the Example 2 and 3 I write on a file with PrintWriter (using write and println methods)
In the example 4 and 5 I link PrintWriter at FileWriter and I use write and println
In the last example I use PrintWriter linked at FileOutputStream to write on a file

The problem is everything prints on a file the same things.
KK
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

john price wrote:

when (and why) I need to use the PrintWriter class (and the PrintStream also)

I generally use PrintWriter for Java networking.


Interesting. I rarely use anything character based in Java networking since most times I work with bytes. When I have used character based networking I have never considered using PrintWriter since it swallows too many exceptions and therefore requires one to explicitly check for errors.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
 
 
subject: About PrintWriter with println and write
 
Similar Threads
Writting into two files
move file A to file B
Java IO Problem
Reg PrintWriter
PrintWriter using flush() and close() for code to work