Updated April 12, 2023
Introduction to Java InputStreamReader
Java InputStreamReader is a subclass of Java Reader class. It is used for interpreting all the bytes of an input stream into a character-based reader which is sometimes used for reading characters from the files where the text represents all bytes from the input text file. Also, it is used to read the character from the byte stream or the file in UTF-8 format only. A wrapper class can be prepared using the InputStreamReader which can be used at some later point of time with FileInputStream in order to read all the characters from the stream.
Syntax
The syntax flow of the InputStreamReader gets started from the declaration followed by the method calling with different syntaxes :
public class InputStreamReaderextends Reader
- public class – The type of class taken is public,
- InputStreamReader – This is the subclass of Reader class which is used for interpreting the bytes or the characters from the class.
- Reader – Reader class is the main class for extending the propertied from InputStreamReader sub-class.
Variations in the syntaxes for different methods is represented as follows:
- public int read() – This is a method of InputStreamReader sub-class which is used for reading the input data streams and further returns one single character after reading.
- public String getEncoding() – This method returns the name of the character encoding being used by this stream.
- public void close() – This method closes the InputStreamReader and then all the associated streams get released.
- public boolean ready() – It is an InputStreamReader which is used for telling whether the character stream is ready to be read even if the Input Buffer for the reader class is having some character or text.
How InputStreamReader work in Java?
InputStreamReader is a sub-class of the Reader class which works in a way where the flow gets started in a way that the java.io package as part of the InputStreamReader class can be used for converting the data and its associated bytes into the characters of the data and the stream. It extends the abstract class of the reader class and the sub-class InputStreamReader works with the other input streams which is used for bridging the gap between character streams and byte streams and hence is used for reading the bytes from the input stream and then gets converted into characters. For instance a storage class is used to store the data and further to read the data that gives an output that is used for reading the accumulated bytes of the characters from the file.
A clear-cut flow is explained in a way where first an InputStreamReader is created from the java.io package followed by creating an input stream and then the input is put with the help of Input Stream in the readable format of data to byte stream with numerical representation in UTF-8 format. After that all the methods of InputStreamReader is called one after another for manipulation of data.
Constructors
These are the constructors associated with the Java InputStreamReader class :
InputStreamReader(InputStream inpt_st)
This constructor is used for creating an InputStreamReader which is responsible for reading the data using default charset.
InputStreamReader(InputStream inpt_st, CharsetDecoder decdr)
This constructor is used for creating an InputStreamReader which is responsible for reading the data using a given charset decoder.
InputStreamReader(InputStream inpt_st, Charset chr_set)
This constructor is used for creating an InputStreamReader which is responsible for reading the data using given charset.
InputStreamReader(InputStream inpt_st, String chrset_nm)
This constructor is used for creating an InputStreamReader which is responsible for reading the data using a given named charset.
Methods
- Ready
- Read
- Read
- getEncoding
- close
Methods with its syntax
Below are the respective syntax of the above mentioned methods:
1. public boolean ready(): This method tells whether the stream is in the ready state to get read from the storage.
2. public int read(): This method is used for reading a single character with UTF-8 type.
3. public int read(char[], cbuf, int offset, int length): This method is used to read characters in some portions of an array.
4. public String getEncoding(): This method returns the name of the character encoding used by this stream.
5. public void close(): This method releases all the resources once the stream is set to close and release all the associated resources.
Examples of Java InputStreamReader
Here are the following examples mention below:
Example #1
This program illustrated to read the content of the file using the InputStream Reader read() method.
Code:
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class Inpt_Stream_Read {
public static void main(String[] args) {
char [] arr = new char[100];
try {
FileInputStream file_a = new FileInputStream("java_File.txt");
InputStreamReader file_b = new InputStreamReader(file_a);
file_b.read(arr);
System.out.println("Opened_stream contains some content. ");
System.out.println(arr);
file_b.close();
}
catch(Exception excpn) {
excpn.getStackTrace();
}
}
}
Output:
Example #2
This program illustrates the encoded value of the file if proper file is encoded it will return the value otherwise will catch for the exception with no such file defined.
Code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class Inpt_stream_Read_Encode {
public static void main(String[] args) {
try {
FileInputStream file_a = new FileInputStream("unusual.txt");
InputStreamReader inptstrm = new InputStreamReader(file_a);
String ancode = inptstrm.getEncoding();
System.out.println("method for encoding includes :" + ancode);
} catch (FileNotFoundException file_not_found_e) {
System.out.println("There is no such file defined for execution.");
}
catch(IOException any_excpn)
{
System.out.println("Some kind of exception occured.");
}
}
}
Output
Example #3
This program is used to illustrate the ready and close method of the input stream used for the input stream.
Code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class Ready_close_input_file {
public static void main(String[] args) {
try
{
FileInputStream once = new FileInputStream("java_File.txt");
InputStreamReader inpt_st = new InputStreamReader(once);
int t;
while((t=inpt_st.read())!= -1)
{
char res = (char)t;
System.out.println("Character value to int value : "+res);
boolean bare = inpt_st.ready();
System.out.println("is it ready : "+bare);
}
inpt_st.close();
once.close();
}
catch (FileNotFoundException fil_not_found_excp)
{
System.out.println("There is not such defined file available. ");
}
catch (IOException for_which_excpt)
{
System.out.println("IOException will occur with this. ");
}
}
}
Output
Conclusion
InputStreamReader is an advantage in Java as it helps in reading and manipulation of the content of the file. It makes use of all the methods of the subclass of the InputStream class which is inherited from the parent class which is a reader class. Hence all the methods introduced in the subclass are useful for any kind of changes and reading or writing and creation of the InputStreamReader buffer as well.
Recommended Articles
This is a guide to Java InputStreamReader. Here we discuss how InputStreamReader works in Java with appropriate syntax, methods, and respective programming examples. You may also have a look at the following articles to learn more –