×

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

&amp;&amp; Operator in Java

“ && ” is the conditional - And operator in Java. In Java, it is an example of a logical operator. In Java, the “ & ” operator has two...

3 minutes read.

Type Annotations in Java

Only declarations were eligible for annotations in earlier versions of Java. With Java 8, you may now annotate any type use, including types in declarations, generics, and casts: @Encrypted String data; List<@NonNull...

6 minutes read.

Basic Terms in Multithreading

To understand the terms of multithreading, we must have knowledge about concurrency, processes, and threads. Concurrency The concurrency stands for performing multiple tasks at the same time. In the process communication, the operating system permits the process...

8 minutes read.

Java List Interface

List interface is used when we have to order a collection which contains duplicate entries. Like an array, the elements of its implementation classes are retrieved and inserted at a...

2 minutes read.

Union in Java

Sets.union() method in Java returns an immutable representation of both the union of two sets. Every element that is present in either backup set is included in the set that...

2 minutes read.

Java LinkedList vs ArrayList

LinkedList In LinkedList, each element is a distinct entity containing an information portion and an address component, and the elements are not kept in consecutive locations. Pointers & addresses are used...

3 minutes read.

Highest precedence in Java

In Java, the operator is the first thing that springs to mind when discussing precedence. The order in which the operators in an expression are evaluated is controlled by a...

3 minutes read.

Java IO file not found exception

One of the exception classes offered by the java.io package is FileNotFoundException. An exception is raised when we attempt to access a file that isn't present in the system. It...

4 minutes read.

Jdoodle Java

Everything is instantaneous and fast-paced in the modern world. Online compilers available via the internet are immensely helpful for programmers seeking to learn a new programming language but lacking the...

4 minutes read.

Java Throw and Throws Keyword

Exceptions in Java enable us to construct high-quality programs where faults are checked at compile time rather than run time and where we may define unique exceptions that make code...

6 minutes read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 minutes read.

Interchange Diagonal elements in Java

In this article, you will be very well acknowledged about what is a matrix with an example. You will also be acknowledged about how to interchange the diagonal elements in...

4 minutes read.

Java String replaceAll() method

Java String replaceAll() method returns a String replacing all the sequence of characters matching regular expression i.e regex and replacement string. Syntax: public String replaceAll(String regex, String replacement) Parameters: regex : regular expression replacement :...

1 minute read.

Java Swing Time Picker

Prerequisites In this tutorial, we will learn the time picker in java swing. Before learn time picker, we should learn about java swing. Java Swing Introduction The Java Foundation Classes include Java Swing....

5 minutes read.

Prime Number Program in Java Using a Scanner

In Java, a prime number is one that can only be divided by one or by itself and is greater than one. In other words, only one or itself can...

3 minutes read.

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

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

Upcasting and Downcasting in Java

Type casting in Java is an important and very interesting topic to deal with. But here upcasting and downcasting is somewhat related to typecasting. In normal typecasting, we convert from...

6 minutes read.

Java Math pow() Method

The pow() method of Math class returns the value of first argument raised to the power of second argument i.e. ab. Syntax: public static double pow(double a, double b) Parameters: The parameters ‘a’ and...

3 minutes read.

Java Else Keyword

The else 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.