see Reader class for descriptions of InputStreamReader's methods. They are:
.mark( readAheadLimit ) Mark the present position in the stream.
.markSupported( ) Tell whether this stream supports the mark(...) operation.
.read( ) Read a single character, returning it in the low end two bytes of an int.
.read( char[ ] ) Read characters into a char array.
.read( char[ ], offset, len) Read characters into a portion of a char array.
.ready( ) Tell whether this stream is ready to be read.
.reset( ) Reset the stream.
.skip( n ) Skip over n characters in the stream.
.close( ) Close the stream.
plus InputStreamReader adds .getEncoding(
) , mentioned below
■ InputStreamReader is the only character stream oriented Reader class whose name contains the byte stream oriented word “InputStream.” That's because it is the only Reader class which can accept byte streams as its source. It converts the bytes to Unicode chars.
■ If you don't change the default encoding, it just converts to the default machine encoding, which may be no conversion at all when UTF-8 is produced. If you do not specify an encoding name, the InputStreamReader uses the default encoding for the default locale, which is usually the right thing to do.
■ InputStreamReader's
most frequent use is to be wrapped by a BufferedReader.
■ InputStreamReader adds one method to those provided by Reader. It is .getEncoding( ) for specifying custom byte to char conversions
■ InputStreamReader has alternate constructors allowing you to specify the encoding of the source InputStream byte stream. The source InputStream stream does not have to be ASCII. Everything is always converted to Unicode however.
■ InputStreamReader supports .mark(...) and .reset( ).
■ Reading from the console. Since in's data type in the System class is an InputStream, and InputStream is a byte stream,
InputStreamReader can be used to read
from the console. In doing so, it
converts incoming bytes from the console, which are in the machine's normal
character encoding mode, into Unicode chars. In this example note that since InputStreamReader's constructor does not
throw an IOException it can be located
outside the try-catch block.
import
java.io.*;
InputStreamReader
isr = new InputStreamReader( System.in );
System.out.println(
" Enter a keyboard character:" );
try
{
int x = isr.read( );
System.out.println("Your character
was: " + (char) x);
isr.close( );
}
catch (Exception ex) { }
■ Converting text files to all caps. The snippet below addresses the fact that
there is no there is no toUpperCase( ) method for bytes - just for Strings and for chars. Here, an InputStreamReader stream is chained to a FileInputStream which reads a byte text
file. The resultant characters produced
by InputStreamReader are then converted to
all caps and printed.
import
java.io.*;
try {
char c;
FileInputStream fis = new
FileInputStream( "somebytefile" );
InputStreamReader isr = new InputStreamReader(
fis );
int x = isr.read( );
while ( x != -1 ) {
c = Character.toUpperCase( (char) x
);
System.out.print( c );
x = isr.read( );
}
fis.close( );
} catch (IOException e) { }