- Java InputStreamReader
- Java InputStreamReader Example
- Creating an InputStreamReader
- Set InputStreamReader Character Encoding
- Get InputStreamReader Character Encoding
- read()
- End of Stream
- Closing an InputStreamReader
- Java io inputstreamreader init
- java.io Class InputStreamReader
- InputStreamReader
- InputStreamReader
- read
- read
- skip
- ready
- markSupported
- mark
- reset
- close
Java InputStreamReader
The Java InputStreamReader class, java.io.InputStreamReader , wraps a Java InputStream, thereby turning the byte based InputStream into a character based Reader . In other words, the Java InputStreamReader interprets the bytes of an InputStream as text instead of numerical data. The Java InputStreamReader class is thus a subclass of the Java Reader class.
The Java InputStreamReader is often used to read characters from files (or network connections) where the bytes represents text. For instance, a text file where the characters are encoded as UTF-8. You could use an InputStreamReader to wrap a FileInputStream in order to read such a file.
Java InputStreamReader Example
Here is a Java InputStreamReader example:
InputStream inputStream = new FileInputStream("c:\\data\\input.txt"); Reader inputStreamReader = new InputStreamReader(inputStream); int data = inputStreamReader.read(); while(data != -1) < char theChar = (char) data; data = inputStreamReader.read(); >inputStreamReader.close();
This example first creates a FileInputStream and then wraps it in an InputStreamReader . Second, the example reads all characters from the file via the InputStreamReader
Note: The proper exception handling has been skipped here for the sake of clarity. To learn more about correct exception handling, go to Java IO Exception Handling.
Creating an InputStreamReader
To create a Java InputStreamReader you simply instantiate it like any other object, passing an InputStream to its constructor. Here is an example:
InputStream inputStream = new FileInputStream("c:\\data\\input.txt"); Reader inputStreamReader = new InputStreamReader(inputStream);
This is all it takes to create an InputStreamReader .
Set InputStreamReader Character Encoding
The characters in underlying InputStream will be encoded using some character encoding. This character encoding is referred to as a character set, or Charset in Java. Two of the commonly used character sets are ASCII (or ISO-Latin1) and UTF8 (or UTF-16 in some cases).
You need to tell the Java InputStreamReader instance what character sets the characters in the InputStream are encoded with. You do so in the InputStreamReader constructor. Here is an example of setting the character set to be used by a Java InputStreamReader :
InputStream inputStream = new FileInputStream("data.txt"); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
As you can see, the character set is passed as the second parameter to the InputStreamReader constructor. In the example above, the character used is UTF-8 .
Get InputStreamReader Character Encoding
You can get the character encoding used by a Java InputStreamReader instance via its getEncoding() method. Here is an example of obtaining the character encoding used by a Java InputStreamReader :
String encoding = inputStreamReader.getEncoding();
read()
The read() method of an InputStreamReader returns an int which contains the char value of the char read. Here is a Java InputStreamReader read() example:
int data = inputStreamReader.read();
You can cast the returned int to a char like this:
End of Stream
If the read() method returns -1, the end of stream has been reached, meaning there is no more data to read in the InputStreamReader . That is, -1 as int value, not -1 as byte or short value. There is a difference here!
When the end of stream has been reached, you can close the InputStreamReader .
You typically read from a Java InputStreamReader inside a while loop, like this:
int data = inputStreamReader.read(); while(data != -1) < char theChar = (char) data; data = inputStreamReader.read(); >inputStreamReader.close();
As you can see, the while loop keeps running until a -1 is read from the InputStreamReader read() method. After that, the InputStreamReader is closed. See the next section for more information about how to close an InputStreamReader correctly.
Closing an InputStreamReader
When you are finished reading characters from the InputStreamReader you should remember to close it. Closing an InputStreamReader will also close the InputStream instance from which the InputStreamReader is reading.
Closing an InputStreamReader is done by calling its close() method. Here is how closing an InputStreamReader looks:
You can also use the Java try with resources construct introduced in Java 7. Here is how to use and close a InputStreamReader looks with the try-with-resources construct:
InputStream input = new FileInputStream("data/text.txt"); try(InputStreamReader inputStreamReader = new InputStreamReader(input)) < int data = inputStreamReader.read(); while(data != -) < System.out.print((char) data)); data = inputStreamReader.read(); >>
Notice how there is no longer any explicit close() method call. The try-with-resources construct takes care of that.
Notice also that the first FileInputStream instance is not created inside the try-with-resources block. That means that the try-with-resources block will not automatically close this FileInputStream instance. However, when the InputStreamReader is closed it will also close the InputStream instance it reads from, so the FileInputStream instance will get closed when the InputStreamReader is closed.
Java io inputstreamreader init
Use is subject to License Terms. Your use of this web site or any of its content or software indicates your agreement to be bound by these License Terms.
Copyright © 2006 Sun Microsystems, Inc. All rights reserved.
java.io
Class InputStreamReader
java.lang.Object | +--java.io.Reader | +--java.io.InputStreamReader
Each invocation of one of an InputStreamReader’s read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.
Fields inherited from class java.io.Reader |
lock |
Constructor Summary | |
InputStreamReader (InputStream is) Create an InputStreamReader that uses the default character encoding. | |
InputStreamReader (InputStream is, String enc) Create an InputStreamReader that uses the named character encoding. |
Method Summary | |
void | close () Close the stream. |
void | mark (int readAheadLimit) Mark the present position in the stream. |
boolean | markSupported () Tell whether this stream supports the mark() operation. |
int | read () Read a single character. |
int | read (char[] cbuf, int off, int len) Read characters into a portion of an array. |
boolean | ready () Tell whether this stream is ready to be read. |
void | reset () Reset the stream. |
long | skip (long n) Skip characters. |
Methods inherited from class java.io.Reader |
read |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
InputStreamReader
Create an InputStreamReader that uses the default character encoding. Parameters: in — An InputStream
InputStreamReader
public InputStreamReader(InputStream is, String enc) throws UnsupportedEncodingException
Create an InputStreamReader that uses the named character encoding. Parameters: in — An InputStream enc — The name of a supported Throws: UnsupportedEncodingException — If the named encoding is not supported
Method Detail |
read
Read a single character. Overrides: read in class Reader Throws: IOException — If an I/O error occurs
read
Read characters into a portion of an array. Overrides: read in class Reader Throws: IOException — If an I/O error occurs
skip
ready
Tell whether this stream is ready to be read. Overrides: ready in class Reader Throws: IOException — If an I/O error occurs
markSupported
public boolean markSupported()
Tell whether this stream supports the mark() operation. Overrides: markSupported in class Reader Tags copied from class: Reader Returns: true if and only if this stream supports the mark operation.
mark
Mark the present position in the stream. Overrides: mark in class Reader Throws: IOException — If an I/O error occurs
reset
close
Close the stream. Overrides: close in class Reader Throws: IOException — If an I/O error occurs Copyright © 2006 Sun Microsystems, Inc. All rights reserved. Use is subject to License Terms. Your use of this web site or any of its content or software indicates your agreement to be bound by these License Terms.
For more information, please consult the JSR 30 specification.