aspose file tools*
The moose likes Beginning Java and the fly likes Reading Character by InputStreamReader, Without type cast? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading Character by InputStreamReader, Without type cast?" Watch "Reading Character by InputStreamReader, Without type cast?" New topic
Author

Reading Character by InputStreamReader, Without type cast?

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


Is it possible by using only InputStream and FileinputStream,without type cast, Read character of a files?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

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.


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

Joined: Sep 06, 2007
Posts: 635


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

Joined: Mar 17, 2011
Posts: 7653
    
  19

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

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
 
wood burning stoves
 
subject: Reading Character by InputStreamReader, Without type cast?