Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Printwriter and OutputStream [duplicate]

Tags:

java

file-io

First,we have PrintWriter

java.io.File f=new java.io.File("s.txt");

   java.io.PrintWriter out=new java.io.PrintWriter(f);

   out.print(5);

   out.print(7);

   out.close();

Then we have outputstream

 java.io.File f=new java.io.File("s.txt");

 java.io.FileOutputStream out=new java.io.FileOutputStream(f);

   out.write(5);

   out.write(7);

   out.close();

Whats the difference?

like image 496
rena-c Avatar asked Jul 30 '11 14:07

rena-c


People also ask

What is the difference between printwriter class and OutputStream class?

For writing byte-oriented informations (such as image etc), we use ServletOutputStream class. It is a byte-stream class. On the other hand, PrintWriter class can only be used to write character based informations.

What is the difference between an OutputStream and a writer?

Whats the difference? OutputStreams are meant for binary data. Writers (including PrintWriter) are meant for text data.

What is the difference between a writer and a printwriter?

Writers (including PrintWriter) are meant for text data. You may not see the difference in your specific situation as you're calling PrintWriter.write (int) which writes a single character - if the character encoding you're using just maps characters to the same byte, for characters less than 127, then you'll see the same result.

Should I use PrintStream or printwriter for writing strings?

If strings are to be written to an OutputStream with a specific charset, PrintStream is the right choice. The reason for this is that PrintStream provides a constructor that accepts an OutputStream and an encoding. The PrintWriter constructor only counts an encoding if a file or a path is specified.


2 Answers

OutputStreams are meant for binary data. Writers (including PrintWriter) are meant for text data.

You may not see the difference in your specific situation as you're calling PrintWriter.write(int) which writes a single character - if the character encoding you're using just maps characters to the same byte, for characters less than 127, then you'll see the same result. But if you give it a different encoding, then you'll see a difference.

PrintWriter is also different in that it suppresses IO exceptions - as does PrintStream, which is the binary stream equivalent of PrintWriter.

like image 193
Jon Skeet Avatar answered Nov 16 '22 01:11

Jon Skeet


From this java2novice.com link I extracted the following, that is similar to what Jon said:

ServletOutputStream: ServletResponse.getOutputStream() returns a ServletOutputStream suitable for writing binary data in the response. The servlet container does not encode the binary data, it sends the raw data as it is.

PrintWriter: ServletResponse.getWriter() returns PrintWriter object which sends character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding(). If the response's character encoding has not been specified then it does default character encoding.

like image 21
Christian Vielma Avatar answered Nov 16 '22 00:11

Christian Vielma