• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PrintStream & PrintWriter

 
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

Thanks for the reply.

Any other option apart from using a loop ?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,

Thanks for the reply.

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandy & Rob,

Thank you so much.
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pramod talekar,
Your post was moved to a new topic.
That is because you asked a new question in an existing thread.
 
Just the other day, I was thinking ... about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic