Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes PrintStream & PrintWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "PrintStream & PrintWriter" Watch "PrintStream & PrintWriter" New topic
Author

PrintStream & PrintWriter

pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hello,

If out in System.out.println(String string) is of type PrintStream which is a part of OutputStream and OutputStream is used for byte streams and not character, then how are we able to print characters ?

I thought PrintWriter would be more suitable .

Please advise.


Thanks,
Pramod
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

The member variable out of class System is indeed of type PrintStream, which is a subclass of OutputStream.

And indeed, in Java, InputStream and OutputStream are for reading and writing binary data; for character data, you should use a Reader or a Writer.

I suspect that class PrintStream is an old, legacy class. The documentation says it exists since Java 1.0. Class PrintWriter was new in Java 1.1. Probably the idea of Readers and Writers for character data was invented in Java 1.1. But for backward compatibility reasons, class PrintStream remained in Java version 1.1 and newer, and the type of System.out still remains PrintStream for compatibility reasons.

When you need to write character data yourself, and you have to decide whether to use PrintStream or PrintWriter, always choose PrintWriter. Note that PrintStream doesn't allow you to choose the character encoding - it will always use the platform's default character encoding, which is not always what you want.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hi Jasper,

Thanks for the reply.

I wonder why FileReader takes so many statements to read from a file compared to FileWriter which takes very few while writing to a file.

In the below code :

Is there any way to avoid BufferedReader as it is the only one which provides readLine which returns String.
Also how to read multiple lines as readLine terminates after reading a line.

Please advise.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

pramod talekar wrote:Is there any way to avoid BufferedReader as it is the only one which provides readLine which returns String.

If you want to read a file line by line you basically have the choice between BufferedReader and java.util.Scanner.

Also how to read multiple lines as readLine terminates after reading a line.

Use a loop:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hi Rob,

Thanks for the reply.

Any other option apart from using a loop ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
Is there really that much difference in the number of lines required to read and to write? The only difference is that you need to use the newLine() method of a BufferedWriter, otherwise the number of lines is the same.

For text files, where it is not necessary to append to the end of the file, consider the Scanner and Formatter classes. No, you cannot read several lines together. You must regard each line as a unit, and there is no such thing as a paragraph.
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hi Campbell,

Thanks for the reply.

sandy chops
Greenhorn

Joined: Nov 11, 2010
Posts: 8
hi Pramod,

If you don't want to use while loop, and if the file size is less then Integer.MAX_VALUE you can use below code


FileInputStream fin = new FileInputStream(file);

Create a byte array to hold the data of the file

byte data= new byte[(int)file.length()];

fin.read(data);

after this you can create a string out of the bytearray and print it or use it.

by this method you don't need to use while loop.

hope this helps
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Except that won't work as InputStream.read(byte[]) is not guaranteed to read all the bytes you want. You'll need to loop again (Jesper de Jong posted code for that here), or create a DataInputStream around the FileInputStream and use its readFully method.
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Sandy & Rob,

Thank you so much.
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hi Jasper,

Coming back to your response,

I suspect that class PrintStream is an old, legacy class. The documentation says it exists since Java 1.0. Class PrintWriter was new in Java 1.1. Probably the idea of Readers and Writers for character data was invented in Java 1.1. But for backward compatibility reasons, class PrintStream remained in Java version 1.1 and newer, and the type of System.out still remains PrintStream for compatibility reasons.

When you need to write character data yourself, and you have to decide whether to use PrintStream or PrintWriter, always choose PrintWriter. Note that PrintStream doesn't allow you to choose the character encoding - it will always use the platform's default character encoding, which is not always what you want.

Does it mean that OutputStream & InputStream were originally written to have both binary & character form and only after 1.1 they were restricted to byte but PrintStream was allowed to have both binary & char?

Also how were we able to write in a char form before Java 1.1 as PW was new in Java 1.1?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

pramod talekar wrote:I wonder why FileReader takes so many statements to read from a file compared to FileWriter which takes very few while writing to a file.

You can combine the statements into fewer lines:

Note: You don't need to close the wrapped FileReader explicitly; BufferedReader will take care of closing it when you call br.close().

sandy chops wrote:If you don't want to use while loop, and if the file size is less then Integer.MAX_VALUE you can use below code

That code does not work correctly, I recently explained exactly why this doesn't work. (Ah, Rob already also noticed it).

pramod talekar wrote:Does it mean that OutputStream & InputStream were originally written to have both binary & character form and only after 1.1 they were restricted to byte but PrintStream was allowed to have both binary & char?

No. OutputStream and InputStream were designed for writing and reading binary data, and before Java 1.1 the people who made Java just didn't think enough about writing and reading characters. PrintStream was an early attempt to make something that could write characters, but it was poorly designed (because it doesn't have a way to specify the character encoding).

pramod talekar wrote:Also how were we able to write in a char form before Java 1.1 as PW was new in Java 1.1?

You could use PrintStream ofcourse. The only problem was that you couldn't use anything else than the platform's default character encoding.
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

So what I understand as of now is that though PrintStream is extended from OutputStream, it has added functionality of handling char as well.
The newly designed Reader & Writer are better than PrintStream for writing char format because of the mentioned reasons by you & also it can't be reverted now to just byte form as that will cause compatibility issue.

Please correct me if I'm wrong and thanks for such a nice and detailed advice.

I'm falling in love with Java ;)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

pramod talekar wrote:So what I understand as of now is that though PrintStream is extended from OutputStream, it has added functionality of handling char as well.

Yes, exactly.
pramod talekar wrote:The newly designed Reader & Writer are better than PrintStream for writing char format because of the mentioned reasons by you & also it can't be reverted now to just byte form as that will cause compatibility issue.

They can't change System.out to a PrintWriter instead of a PrintStream, because then it would not work exactly the same as in old versions of Java, which might give problems with old Java programs that expect it to be a PrintStream. Sun and Oracle have always been very careful about backward compatibility; if there is even only a remote reason that a change would break existing, old code, they will not make the change.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
pramod talekar,
Your post was moved to a new topic.
That is because you asked a new question in an existing thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PrintStream & PrintWriter
 
Similar Threads
how to send console output to a html page
About PrintWriter with println and write
Declaring a PrintWriter object
System.out ?
what is the difference between printStream and PrintWriter?