×

Java InputStreamReader

What is InputStreamReader?

An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can be provided explicitly or by name, or the platform's default charset can be allowed. It comes under the java.io package and extends the abstract class Reader. InputStreamReader is often known as the path which connects streams and character streams as it reads a byte from the given input stream of character.

How to Create InputStreamReader?

Following are the steps to create an InputStreamReader.

Importing the java.io.InputStreamReader package is the first step in creating an InputStreamReader without this package, we cannot implement InputStreamReader. After we've imported the package, we must follow the steps below.

First, we create an InputStreamReader, which has a reference variable has st. Then we need to create a FileInputStream which has a reference variable fl. The following example demonstrates the above lines.

FileInputStream fl = new FileInputStream( path);
InputStreamReaderst   = new InputStreamReader( fl  );

2. The fetched data is stored using default character encoding by implementing this code. We can also define the character encoding as per the file or one's need. The character encoding can be either UTF8 or UTF16. We have specified the character encoding in the file in the following code by using the Charset class.

InputStreamReaderst   = new InputStreamReader( fl, Charset c );

Constructors of InputStreamReader

Following are the Constructors  provided by InputStreamReader:

  • InputStreamReader(InputStreamst)This constructor is implemented when there is a need to create an InputStreamReader that has the default charset.
  • InputStreamReader(InputStreamst, Charset ): This constructor is implemented when there is a need to create an InputStreamReader that has the specified charset.
  • InputStreamReader(InputStreamst, CharsetDecoder dc): This constructor is implemented when there is a need to create an InputStreamReader that has the specified charset decoder.
  • InputStreamReader(InputStreamst, String name): This constructor is implemented when there is a need to create an InputStreamReader that uses the named charset

Methods of InputStreamReader

Following are the methods provided by InputStreamReader.

  • ready()

This is a method with a return type. This method is implemented when there is a need to check whether the character stream is ready to perform its function ( i.e., whether it can read or not ).

An InputStreamReader is considered ready to perform its function, If an InputStreamReader's input buffer is empty or bytes are available to be read from the underlying byte stream.

  • read(char[] arr)

It is used when it is necessary to read characters from a reader and store them in a predefined array.

  • read(char[] arr, int st, int l)

It is used when several characters equal to length must be read from the reader and stored in the specified array starting at the beginning.

  • close( )

This method is implemented when there is a need to close cInputStreamReader and releases all the Streams attached to it.

 Once this method is implemented, the stream gets closed. Thus, methods such as ready( ), ready(), and other mentioned methods in the articles won't perform their function and will throw an IOException.

  • getEncoding() Method

This method is implemented when there is a need to acquire the type of encoding is implemented to save the fetched data in the input stream.

  • mark()

When it's necessary to mark the point in the stream where data has been read, this method is implemented

  • reset()

This is a method with a return type. It returns the control to the position where the mark was set in the stream.

import java.io.InputStreamReader;
import java.io.FileInputStream;
public class a {
public static void main(String[] args) {
char[] arr = new char[14];


        try {


            FileInputStream fl = new FileInputStream("d://sample.txt");




InputStreamReader File_input = new InputStreamReader(fl );




File_input.read(arr);
System.out.println("File contains the following data:");
System.out.println(arr);




input.close();
}


catch(Exception e) {
            e.getStackTrace();
}
    }
}

In the above code, first, we imported the important packages to implement FileInputStream and InputStreamReader.

Then we created an array of characters as reference variable as arr and size of 14 as it is the size of the data inside the file.

Then we created a FileOutputStream in which as reference variable as fl and contains specified file path as d://sample.txt. And created an InputStreamReader, a reference variable as File_input, and a default encoding.

Then we read the characters from the file stores them in an array of arrays. To see the data inside the file, we have to print the array. Following is the output obtained when the above code is implemented.

What is InputStreamReader?

The file contains the following text: Hello Everyone, and the same text is shown in the output.


Related Topics

How to Concatenate Two Strings in Java

How to Concatenate Two Strings in Java Concatenation of two strings means adding the beginning of one string to the end of the other string. Some of the ways to concatenate...

3 minutes read.

PriorityQueue in Java

PriorityQueue in Java A PriorityQueue is a member of the Java Collection Framework and is used when the values are needed to be processed based on priority. Priority queue operates similar to...

8 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

Java FileOutputStream

What is FileOutputStream?When we need raw stream data written into a file, we need to look for another option: FileOutputStream. It is used when the file's data is byte-oriented. It comes under...

4 minutes read.

Java Integer max() method

The max() method of Integer class returns the greater of two int values. It returns the same result as by calling Math.max. Syntax public static int max(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Mutable and Immutable in Java

Java is a programming language in which everything is treated as an object. Its procedures and functions are centred around objects because it is an object-oriented programming language. Mutable and...

6 minutes read.

Java Binary to Hexadecimal

Converting between types in programming is an important task. Moving from one kind to another kind conversion is occasionally necessary. We have discussed numerous conversion types in the section on...

3 minutes read.

Java Char Keyword

The java char keyword is a data type which is defined as character data type.Char keyword belongs to a primitive data type where the data types are classified into primitive...

4 minutes read.

Snake and Ladder Problem in Java

Find the smallest number of dice throws necessary to reach the destination or last cell from the source or first cell on a snake and ladder board. Essentially, the player...

6 minutes read.

Java Map Example

In Java, the Map is an interface that is used mainly to denote key and value pairs. The central concept and theme of this mapping in the java collection framework...

4 minutes read.

Minimum Lights to Activate Java Snippet Class

Minimum Lights to Activate Problem in Java In prison, there is a hallway that is N units long. Given an N-dimensional array A. If the light at the ith position is...

3 minutes read.

Addition Program in Java

Addition Program in Java We can perform the addition (sum) of two or more numbers by using the arithmetic operator (+). To write an addition program in Java, one must understand...

3 minutes read.

How to generate random numbers in Java

Random numbers, also known as fake numbers, are actually a part of a very large sequence, so they are called random numbers. In a defined set of numbers, every number...

6 minutes read.

Check whether Java is installed or not

As we know that there are various operating systems, to check whether Java is installed or not in Windows and Mac we use the following ways.           Windows Operating System: There are several...

2 minutes read.

Types of Logical Operators in Java

In this tutorial, we are going to study logical operators. A logical operator is an operator that accomplishes a logical operation that is to connect two or more operations. These...

5 minutes read.

Java Enum Keyword

Definition: A data type in Java called Enum has a respect to supply of constants. The weekdays (SUN, MON, TUE, WED, THU, FRI, and SAT), directions (NORTH, SOUTH, EAST, and WEST),...

4 minutes read.

How to calculate time complexity of any program in Java

Java : Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is...

3 minutes read.

Types of JDBC Drivers

JDBC Drivers: A piece of software known as JDBC Driver permits database communication between Java applications and the server. In order to communicate with our database server, JDBC drivers put into practice...

4 minutes read.

Bouncy Number in Java

We will define bouncy numbers in this section and write Java programmes to determine whether a specific number is bouncy. Java coding exams and academic assignments usually inquire about the...

3 minutes read.

Dictionary in Java

Dictionary in Java The Dictionary class represents a key-value relation which maps keys to values. In Dictionary class, every key and every value is an object. It is an abstract class associated with...

2 minutes read.