×

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

String vs StringBuilder

String vs StringBuilder In this section, we will discuss the comparison between Java String and StringBuilder class. String In Java, a string is treated as an object that represents a sequence of characters....

4 minutes read.

Concurrent Modification Exception In Java

When an object is attempted to be updated concurrently when it is not allowed, the ConcurrentModificationException arises. This error typically occurs while using Java Collection classes. When another thread is iterating...

5 minutes read.

Java Comparable Interface

We implement comparable interface when we need a new logic for determining equality. if two objects are equal, the equals() method returns true and compareTo() method returns 0. The basic...

1 minute read.

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.

Java If Keyword

Definition: The if statement specifies a section of Java code that will run if an if statement's condition is false. The following conditional statements can be used in Java: To provide a block...

3 minutes read.

How to Reverse a String in Java

How to Reverse a String in Java There are a lot of ways to reverse a string in Java. One can use iteration, StringBuilder, StringBuffer to do the reverse of a...

6 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

Java String Methods

Java String Methods Java String class is the most important class of the java.lang package. It is used to handle the String related operations. It contains a lot of built-in Java...

2 minutes read.

Frugal number in Java

A frugal number is a positive integer with base b that has more digits than the number of prime factors it can be factored into (including exponents greater than 1)....

3 minutes read.

Best Practices to use String Class in Java

Use String Builder or String Buffer for String concatenation in place of + operator.Compare two strings by equals( ) method instead == operator.Call .equals( ) method on a known String...

3 minutes read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 minutes read.

Class vs Object in Java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Fork Join in Java

Multithreaded processors are being introduced in new computer systems today. The operation is faster due to multicore CPUs. Therefore, it becomes essential for a programmer to leverage multithreaded processors effectively...

4 minutes read.

Java Substring

What is a substring in Java? Here by the name  " substring " itself, we can easily come to know it is a part of a string or a subset of...

4 minutes read.

File Operation in Java

In Java, a file is an Abstract data type. These are used to store the data which is related. Files are named storage locations. With a file we can perform...

7 minutes read.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method...

2 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

House Numbers in Java

In this section, we will discuss about house number in Java. It is a sum of cubes, each of which has a dimension of h + 1. There is a...

3 minutes read.

Java Math nextDown() Method

The nextDown() method of Math class returns the floating-point number adjacent to the argument in direction of the negative infinity. Syntax: public static double nextDown (double d)public static float nextDown (float f) Parameters: The...

2 minutes read.