File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes what is the difference between printStream and PrintWriter? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "what is the difference between printStream and PrintWriter?" Watch "what is the difference between printStream and PrintWriter?" New topic

what is the difference between printStream and PrintWriter?

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
what is the difference between PrintStream and PrintWriter?

SCJP 1.4
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by saikrishna cinux:
what is the difference between PrintStream and PrintWriter?

Haven't you Google for this information....

Also check out this link.. Character streams

---From JAVA API Specifications

public class PrintStream
extends FilterOutputStream

A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Two other features are provided as well. Unlike other output streams, a PrintStream never throws an IOException; instead, exceptional situations merely set an internal flag that can be tested via the checkError method. Optionally, a PrintStream can be created so as to flush automatically; this means that the flush method is automatically invoked after a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written.

All characters printed by a PrintStream are converted into bytes using the platform's default character encoding. The PrintWriter class should be used in situations that require writing characters rather than bytes.

public class PrintWriter
extends Writer

Print formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.

Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println() methods is invoked, rather than whenever a newline character happens to be output. The println() methods use the platform's own notion of line separator rather than the newline character.

Methods in this class never throw I/O exceptions. The client may inquire as to whether any errors have occurred by invoking checkError().

Tell us what exactly you don't understand....We will appreciate your kind efforts regarding this issue......

But first of all come up with exact problem ...
[ September 12, 2006: Message edited by: Ankur Sharma ]

The Best way to predict your future is to create it - Every great individual common man
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
PrintStream is an older class, from before Writer was defined. PrintStream is an OutputStream, which means it is mainly intended for processing bytes. PrintWriter is a Writer, meaning it is intended for processing characters. As the "printing" operations that both classes perform are more to do with characters, PrintWriter is usually preferred over PrintStream.

If what you want to do is to do "printing" operations and the ultimate destination is an OutputStream, use an OutputStreamWriter between your PrintWriter and your OutputStream.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I agree. Here's the link:
subject: what is the difference between printStream and PrintWriter?
It's not a secret anymore!