Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading Character by InputStreamReader, Without type cast?

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is it possible by using only InputStream and FileinputStream,without type cast, Read character of a files?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. If you need to read characters (text), you're doing it correctly by using a Reader.

Java has two kinds of classes for I/O: streams (class InputStream and OutputStream and everything that extends those) and readers / writers (class Reader and Writer and everything that extends those). Streams are for reading binary data (bytes), exactly as they are in a file. Readers and writers are a layer on top of that, to translate the bytes to and from text characters (using a character encoding).

I don't know why the read() method of Reader returns an int instead of a char. That's probably an old design mistake in class Reader.

Note that class Reader has other methods to read characters into a character array. You could make an array of one character, call the appropriate read() method and get the character without casting, but that's more cumbersome than the code you already have, with a cast.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This program can read text file.



This program can read text files too. we use type cast in both.Whats is advantage of using inputstreamreader from Reader class?
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:This program can read text files too. we use type cast in both.Whats is advantage of using inputstreamreader from Reader class?

Because it is designed specifically for reading text, which has a myriad of encodings. An InputStream is for reading bytes.

And furthermore, you should generally buffer them for best performance (although the File... classes may alrerady be buffered).

Winston
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second set of programs, in which you use only a FileInputStream, will not always work correctly. They will work if the text is ASCII-encoded, but not for other encodings.

In your second set of programs you are directly casting bytes read from the file to characters. You're not translating the bytes using a character encoding. In Java, that's what a Reader (such as FileReader) does for you.

Here's a good article about what character encodings are:

What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic