The InputStreamReader
is a class in the Java programming language that allows for reading bytes from an input stream and converting them into characters using a specified character encoding.
It is a bridge between byte-oriented streams and character-oriented streams. By default, it uses the platform’s default character encoding to convert bytes to characters, but you can specify a different character encoding if needed.
Here’s an example of how to use the InputStreamReader
class to read characters from an input stream:
try { FileInputStream inputStream = new FileInputStream("input.txt"); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); int character; while ((character = inputStreamReader.read()) != -1) { System.out.print((char) character); } inputStreamReader.close(); } catch (IOException e) { e.printStackTrace(); }
In this example, an InputStreamReader
is created to read characters from a file named input.txt
using the UTF-8 character encoding. The read()
method is called in a loop to read one character at a time from the input stream, and the characters are printed to the console.
It’s important to note that the InputStreamReader
should be closed after use to release any system resources associated with it. This can be done by calling the close()
method on the InputStreamReader
.
Constructor:
The InputStreamReader
class provides several constructors to create instances of the class:
InputStreamReader(InputStream in)
: Creates anInputStreamReader
that uses the default character encoding to convert bytes from the specified input streamin
to characters.InputStreamReader(InputStream in, String charsetName)
: Creates anInputStreamReader
that uses the specified character encodingcharsetName
to convert bytes from the specified input streamin
to characters.InputStreamReader(InputStream in, Charset cs)
: Creates anInputStreamReader
that uses the specified character setcs
to convert bytes from the specified input streamin
to characters.InputStreamReader(InputStream in, CharsetDecoder dec)
: Creates anInputStreamReader
that uses the specified character decoderdec
to convert bytes from the specified input streamin
to characters.
Here’s an example of how to use the second constructor to create an InputStreamReader
that uses the UTF-8 character encoding to read bytes from an input stream:
try { FileInputStream inputStream = new FileInputStream("input.txt"); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); // use the InputStreamReader to read characters from the input stream } catch (IOException e) { e.printStackTrace(); }
In this example, an InputStreamReader
is created using the FileInputStream
constructor to read bytes from a file named input.txt
, and the UTF-8 character encoding is specified as the character set to be used for converting the bytes to characters.
Method:
The InputStreamReader
class provides several methods to read characters from the input stream:
int read() throws IOException
: Reads a single character from the input stream and returns it as an integer. Returns -1 if the end of the input stream has been reached.int read(char[] cbuf, int offset, int length) throws IOException
: Reads up tolength
characters from the input stream and stores them in the specified character arraycbuf
starting at the specifiedoffset
. Returns the number of characters read, or -1 if the end of the input stream has been reached.boolean ready() throws IOException
: Returnstrue
if the input stream is ready to be read,false
otherwise.void close() throws IOException
: Closes theInputStreamReader
and releases any system resources associated with it.
Here’s an example of how to use the read()
method to read characters from an input stream:
try { FileInputStream inputStream = new FileInputStream("input.txt"); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); int character; while ((character = inputStreamReader.read()) != -1) { System.out.print((char) character); } inputStreamReader.close(); } catch (IOException e) { e.printStackTrace(); }
In this example, the read()
method is called in a loop to read one character at a time from the input stream, and the characters are printed to the console.
You can also use the read(char[] cbuf, int offset, int length)
method to read multiple characters at once into a character array. This can be more efficient than reading one character at a time.
Example:
Here’s an example that demonstrates how to use the InputStreamReader
class to read characters from a file and write them to a new file, with a different character encoding:
import java.io.*; public class InputStreamReaderExample { public static void main(String[] args) { try { // Create an InputStreamReader to read characters from a file FileInputStream inputStream = new FileInputStream("input.txt"); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); // Create an OutputStreamWriter to write characters to a new file, with a different encoding FileOutputStream outputStream = new FileOutputStream("output.txt"); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "ISO-8859-1"); // Read characters from the input file and write them to the output file int character; while ((character = inputStreamReader.read()) != -1) { outputStreamWriter.write(character); } // Close the input and output streams inputStreamReader.close(); outputStreamWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }
In this example, an InputStreamReader
is created to read characters from a file named input.txt
, using the UTF-8 character encoding. An OutputStreamWriter
is also created to write characters to a new file named output.txt
, using the ISO-8859-1 character encoding.
Characters are read from the input file and written to the output file one at a time, using the read()
and write()
methods. Finally, the input and output streams are closed to release any system resources associated with them.
This example shows how the InputStreamReader
can be used in conjunction with the OutputStreamWriter
to convert characters from one character encoding to another.