×

Java String Reader

StreamReaderClass:

This class is present in the java.io package. It is a character stream in which string act as a source.This method provides to read characters from a string.

Character Stream:

  • This 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.
  • This stream is divided into types of classes. They are:
  • Reader class
  • Writer class.

Reader class:

  • It is used to read characters from the input stream.
  • It throws an IO Exception while reading the input.

Writer class:

  • It is used to write characters to the output stream.
  • Methods present in class throw IO Exception.

Declaration of StringReader class:

  public class StringReader extends Reader;

Methods in StringReader class:

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

read():

  • 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:

J
A
V
A
  • It is used to close the character stream.
  • Return type of close() method is void.
  • If the stream has been closed then performing methods like read() and other methods throw an IOException.

Syntax:

  public void close()

Program:

Import java.io.*;
class Example15
{
public static void main(String s[]) throws IOException
{
String string1 = “JavaProgramming”;
StringReader String2=new StringReader(string1);
System.out.print(String2.read());
String2.close();
}
}

Output:

J

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

  • It stores the reading characters in the array form.
  • It returns the count of characters.
  • Integer is the return type.

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 Example2 {
public static void main(String[] s) throws IOException {
String s2 = "JAVA";
StringReader s2=new StringReader(s1);
char c[]=new char[10];
try{
s2.read(c,0,5);
System . out .  println(c);
c.close();
}
catch (IOException E){
System. out. println(E);
}
}

    Output:

     Java

skip (long s):

  • It returns the number of skipped characters from the stream.
  • Long is the return type.

Syntax:

   public long skip(long s)

Program:

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

Output:

JPri

ready():

  • It checks whether the character stream is ready to be read or not.
  • Return type of ready() method is boolean.

Syntax:

public boolean ready();

Program:

import java.io.*;
    class Example18
    {
      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:

Print true if it is ready otherwise false: true

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 Example17{  
  public static void main(String[] s) throws IOException{  
    StringReader string1= new StringReader("Apple");  
    string1 . mark(1);  
    System . out . println(string1.read());
     string1 . close();  
    
  }  
} 

  Output:

65

reset ():

  • 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 Example10 
{  
  public static void main(String[] s) throws IOException
{  
    StringReader string1 = new StringReader("Coding");  
    string1.mark(1);  
    System . out . println(string1 . read());
     string1 . reset();
     string1 . close();  
    
  }  
}

Output:

67

mark supported ():

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

Syntax:

 public void markSupported()

Program:

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

Output :

Print True if it supports otherwise False: true

Related Topics

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 minutes read.

Callable Statement in Java

The Callable statement in Java is used to call the functions and Stored procedures. Example: If we want to know about the age of a person based on their date of birth,...

3 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

Matrix Multiplication Program in Java

Matrix Multiplication Program in Java The matrix multiplication program in Java is the continuation of the matrix program in Java that we have already discussed earlier. In this section, we will...

3 minutes read.

Bifunction in java 8

A functional interface in Java is called BiFunction. It first appeared in Java 8. It can serve as the assigning target for a method reference or lambda expression. The java.util.function...

4 minutes read.

Java vs Scala

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

4 minutes read.

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

6 minutes read.

Java ResultSetMetaData

The data about another data is called Metadata. The ResultSetMetaData is used to store the data about ResultSet. The ResultSet Contains the columns, rows, names of table, datatypes etc. these...

2 minutes read.

How to Calculate Week Number From Current Date in Java?

The WeekFields class's weekOfMonth() method is utilized to return the field for access the week of a month based on this WeekFields. If the first day of the month is a...

3 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.

Difference between Static and Instance Methods in Java

Static Method in Java The static technique has a place with the class as opposed to the object of the course. These are intended to be divided between every one of...

10 minutes read.

Java String length() method

Java String length() method returns length of the characters in a String. Syntax: public int length() Returns It returns length of the characters Java String length() method example 1          public class JavaStringLengthEx1  ...

1 minute read.

Java callable example

What is callable in Java? Callable is an interface that is present in Java. There are two methods for constructing threads: one is to inherit the Thread class, while the other...

3 minutes 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 Integer signum() method

The signum() method of Java Integer class returns the signum function of the specified int value. Syntax public static int signum (int i)  Parameters The parameter ‘i’ represents the value whose signum is to...

1 minute read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

Java Math log() Method

The log() method of Math class returns the natural logarithmic value for the specified double argument. Syntax: public static double log(double a) Parameters: The parameter ‘a’ represents the value. Return Value: The log() method returns the...

1 minute read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

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.