×

Java StringReader Class

StreamReader Class technique enables character reading from a string. The java.io package contains this class. A string serves as a source in the character stream. While the Stream class is made for byte input and output, StreamReader is made for character input with a certain encoding.

Character Stream

Byte streams are frequently "wrapped" in character streams. While handling the physical I/O, the character stream makes use of the byte stream to translate between characters and bytes. The superclasses of all character stream classes are the Reader and Writer classes. The class is provided by java.io. Package. This class is used to overcome the limitations of the byte stream. The main purpose is to read characters and write the characters

MethodDescription
int read()It is used to read a single character.
int read(char[] cbuf, int off, int len)It is used to read a character into a portion of an array.
boolean ready()It is used to tell whether the stream is ready to be read.
boolean marksupported()It is used to tell whether the stream support mark() operation.
long skip(long ns)It is used to skip the specified number of character in a stream
void mark(int readAheadLimit)It is used to mark the mark the present position in a stream.
void reset()It is used to reset the stream.
void close()It is used to close the stream.

 Reader class: Reader class is used to read characters from the input stream.

Declaration of StringReader class

  // StringReader Creation

StringReader input = new StringReader(String data);

Methods in StringReader class

  • read()
  • close()
  • read(char[] c, int start, int length)
  • skip(longs)
  • ready()
  • mark()
  • reset()
  • mark supported()

read() method

  • It is used to read single characters from the stream.
  • It returns -1 if it reaches the end of the file or end of input.
  • It represents whether the next character is present or not.
  • Return type of read() method is int(Integer).

Syntax:

public int read()

Program:

import java.io.*;
 class Example1 {
   public static void main(String[] s) throws IOException {
      String s1 = "JAVA";
      StringReader s2=new StringReader(s1);
      for(int i=0;i<s1.length();i++)
      {
      char c=(char) s2.read();
      System . out . println(c);
      }
      s2.close();
   }
}

Output

Java StringReader Class

close() method

  • Close method is used to close the character stream.
  • If the stream has been closed, performing methods like reading (), readLine and other methods throw an IOException.

Syntax:

public void close()

Program

import java.io.*;
 class Example1 {
   public static void main(String[] s) throws IOException {
      String s1 = "JAVA";
      StringReader s2=new StringReader(s1);
      for(int i=0;i<s1.length();i++)
      {
      char c=(char) s2.read();
      System . out . println(c);
      }
      s2.close();
   }
}

Output:

Java StringReader Class

read(char[] c, int start, int length) method

  • It stores the reading characters in the array form.
  • It returns the count of characters.

Syntax:

public int read(char[] c, int start, int length)

c -It is the Destination buffer

start – at which index it starts writing characters.

Length -.Several characters.

Program

import java.io.*;
 class Main 
 {
   public static void main(String[] s) throws IOException 
   {
      String s2 = "JAVAProgramming";
      StringReader s3=new StringReader(s2);
      char c[]=new char[10];
      try
      {
        s3.read(c,0,5);
        System.out.println(c);
      }
    catch (IOException E)
    {
        System.out.println(E);
    }
   }  
 }

    Output:

Java StringReader Class

skip (long s) method

  • It returns the input that matches the specified pattern, ignoring delimiters.

Syntax:

public long skip(long s)

Program

import java.io.*;
class Main
{
  public static void main(String s[]) throws IOException
 {
   String s1="JavaProgramming";
   StringReader s2= new StringReader(s1);
   for (int i=0;i<4;i++) {  
            char c =(char)s2.read();
            s2.skip(3);  
  System.out.print(c);
         }         
   }  
}

Output:

Java StringReader Class

ready() method

  • It checks whether the input stream is ready to be read.
  • Return ready() method is boolean.

Syntax:

public boolean ready();

Program:

    import java.io.*;
    class Main
    {
      public static void main ( String s[]) throws IOException
     {
        String string1="Java";
        StringReader string2=new StringReader(string1);
        System.out.println("Print true if it is ready otherwise false:"+string2.ready());

Output:

Java StringReader Class

mark(int Limit)

  • This method is used to represent the present position in the stream.
  • Void is the return type.

Syntax: public void mark(int Limit)

Program

import java.io.*;  
class Main{  
  public static void main(String[] s) throws IOException{  
    StringReader string1= new StringReader("Apple");  
    string1.mark(1);  
    System.out.println(string1.read());
     string1.close();  
  }  
}

  Output:

Java StringReader Class

reset () method

  • It is used to reset the stream with the most recent mark.
  • Return type of reset(0 method is void.

Syntax:

public void reset () throws IOException.

Program

import java.io.*;
 class Main
{  
  public static void main(String[] s) throws IOException
{  
    StringReader string1 = new StringReader("Java");  
    string1.mark(1);  
    System.out.println(string1.read());
     string1.reset();
     string1.close();   
  }  

Output:

Java StringReader Class

mark supported () method

  • It is used to check whether it supports the mark() method.
  • void is the return type.

Syntax:

public void markSupported()

Program

import java.io.*;
class Main
 {
    public static void main(String[] s) throws IOException
    {
        try 
        {
            String s1 = "JavaT";
            StringReader s2= new StringReader(s1);
             System.out.println("Print True if it supports otherwise False:"+ s2.markSupported());
            s2.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
 
    }
}

Output

Java StringReader Class

Related Topics

Java Long Keyword

Long is a primitive data type in Java. To initilize variables, we implement the long keyword. It can also be applied to techniques. A 64-bit two's complement integer can put...

3 minutes read.

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

Java Byte Code

Java byte code is really a powerful mechanism which makes Java a portable and platform-independent programming language. There are two software components which go along and make this byte code...

3 minutes read.

Java String charAt() method

It returns the char value present in the string at the specified index. Here, index value can not be greater than length() -1. Syntax: public char charAt (int index) Parmeters It accepts only...

3 minutes read.

Java Tokens

Classes and methods are included in the Java program. The procedures also provide the expressions and statements required to finish a specific operation. Tokens make up the sentences and expressions...

4 minutes read.

Java Try-Catch Block

Java Try Block The handling of the exceptions in a block of code is done with the help of java try block. It throws the code that is enclosed in a...

3 minutes read.

Sort Elements by Frequency in Java

To sort the elements in Java by using frequency, we need an input array. We should create a function that sorts the elements in an array by using their frequencies...

3 minutes read.

Intersection Point of two linked list in Java

In this article, you will be very well acknowledged about how to achieve the intersection point of two linked list in Java. There are several approaches to obtain. Surely each...

8 minutes read.

What is String in Java?

What is String in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a Java...

4 minutes read.

GCD of Different SubSequences in Java

The positive numbers are provided in an array called inArr. The aim is to determine the number of distinct GCDs (Greatest Common Divisors) in each subsequence present in the input...

4 minutes read.

Java Final Keyword

In Java, the last keyword is used to limit the user. The applications of the java final keyword have large range of usage in program development. Last can be: variablemethodclass A final...

3 minutes read.

Java Integer shortValue() method

The shortValue() method of Java Integer class returns a short value for this Integer after a narrowing primitive conversion. Syntax public short shortValue () Parameters NA Overrrides: The shortValue () overrides : shortValue in class Number  Return Value This...

1 minute read.

Different Ways to Print Exception Message in Java

In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the...

4 minutes read.

Java vs Node.js

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Java Math sin() Method

The sin() method of Java Math class returns the trigonometric sine of the specified angle. Syntax: public static double sin(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The sin() method...

1 minute read.

Zigzag Array in Java

In this tutorial, we discuss, what is zigzag array and its example. Even we will create the java program. In this program, we convert the simple array into a zigzag...

4 minutes read.

Package Program in Java

Package Program in Java The package program in Java helps us to understand the significance of packages in Java. Packages are mainly used to group together similar classes, interfaces and sub-packages....

6 minutes read.

Three-way operator in Java

The ternary operator in Java is the only conditional operator that takes three operands. It's a popular one-line substitute for the if-then-else expression among Java programmers. If-else clauses can be...

3 minutes read.

How to compare characters in Java

In this tutorial, we will learn about how to compare characters in Java. To compare characters in Java, we will learn about what is a character in Java Char The character is...

4 minutes read.

Difference between String Tokenizer and split Method in Java

Introduction Today, let us understand the difference between String Tokenizer and Split Method. First, let us learn about the String Tokenizer and Split method individually and then know about their differences String...

7 minutes read.