×

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 Solid Principles

Java implements the object-oriented SOLID principles for the design of software architecture. Solid Principles Java implements the object-oriented SOLID principles for the design of software architecture.   Five guiding principles transformed...

4 minutes read.

Arithmetic exception in Java

Exception Handling is one of the most potent ways of handling runtime faults and preserving the application's normal flow. In Java, an exception is an out-of-the-ordinary state, and exceptions are...

3 minutes read.

How to run Java Program?

How to run Java Program In this section, we will learn how to write, compile and run a Java program in Command Promptusing notepad. In order to run a Java program, we...

2 minutes read.

Java Imageio

The javax.imageio package contains the final class known as Java ImageIO. For easy image reading, writing, and simple encoding and decoding, the class offers a convenient way. The class offers...

4 minutes read.

How to Convert Octal to Decimal in Java

How to Convert Octal to Decimal in Java There are two methods to convert Octal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method The Integer.parseInt() method is a static method...

2 minutes read.

Java String intern() method

Java String intern() method returns canonical representation for the String Object. syntax: public String intern() Return: It returns a interned String that to be a pool of unique String. Java String intern() Example 1 public class...

2 minutes read.

Java Math abs() Method

The abs() method of Math class returns the absolute value of the argument where the argument can be int, double, float, long. Syntax public static int abs(int a) public static float abs(float a) public...

2 minutes read.

Java Integer toBinaryString() method

The toBinaryString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 2. Syntax public static String toBinaryString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Java Switch string

A multi-way branch statement is the switch statement. It offers a simple method for allocating execution to various code sections according to the expression's value. Primitive data types, including bytes,...

3 minutes read.

Program to Implement FLAMES Game in Java

Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to...

4 minutes read.

Java 9 Try With Resources

Java 9 provides the improvement in the try statement. It allows us to declare a try statement with duly declared resources. Whenever the user does not require the functionality with...

3 minutes read.

How to handle NullPointerException in Java

Introduction: Throwable class is the super or root class of the java exception hierarchy which contains two sub-classes. The classes’ are- ExceptionError Exception: If you are doing any wrong things in a...

3 minutes read.

Java Boolean logicalXor() Method

The logicalXor() method of Java Boolean class returns the result of implementing logical XOR operation on the specified Boolean operands. Syntax: public static boolean logicalXor (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Java Public Keyword

A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A...

4 minutes read.

Pernicious Number in Java

If the number of 1s in a number appearing in the binary representation is the prime number, such a number is known as a pernicious number. A pernicious number always corresponds to...

4 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.

Swing Program in Java

Java Swing is part of Java Foundation Classes (JFC). The swing toolkit is used to generate Graphical User Interface (GUI) for programs written in Java. The Java swing API is...

4 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

Minimum Window Subsequence in Java

In this article, you will be very well acknowledged about the minimum window subsequence, what is the approach and how it is implemented. The example program is also executed and...

4 minutes read.

Tree Implementation in Java

Introduction to Tree A non-linear, hierarchical data structure called a "tree" is made up of a number of nodes, each of which contains a values and a sequence of pointers to...

14 minutes read.