Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the difference between printStream and PrintWriter?

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between PrintStream and PrintWriter?
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic