×

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

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.

Java Obfuscator

Obfuscation is the process of making something unclear or difficult to interpret. Obfuscators are being used in programming to protect the source code from hackers. In this article, we will...

8 minutes read.

Figurate Number in Java

There have been several uses for figurate or figural numerals throughout history. A number that may be expressed by regular, distinct geometric shapes with spaced evenly points is referred to...

4 minutes read.

How to override toString() method in Java?

Java is an object-oriented language. It only works with classes and objects. Thus, whenever we need to calculate, we need an object or objects that belong to the class. The Java method...

2 minutes read.

Tribonacci series in Java

The Fibonacci series and the Tribonacci sequence are linked. The Fibonacci sequence, each element summates the three preceding terms, is expanded into the Tribonacci series in Java. Through specific examples,...

3 minutes read.

Arrow Operator in Java

The introduction of arrow functions in ES6 gives you a more precise approach to define JavaScript functions. We can write shorter function syntax thanks to them. Your code will be...

4 minutes read.

Count of Range Sum Problem in Java

In this article, we will discuss the basic approach or native approach used to count range sum problem in java. To solve this problem, we will check for numbers if they...

6 minutes read.

Java Generate UUID

What java Generate UUID? A 128-bit long value that is universally unique serves as the basis for the terms UUID (Universally Unique Identifier) and GUID (Globally Unique Identifier). Hexadecimal (octet) digits...

5 minutes read.

Normal and Trace of a Matrix in Java

Normal of a matrix The square root of the total squares of each element in a matrix is known as the matrix's normal. Think of the following matrix as an example. Example: [[1,2,3]  ...

3 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

How Many Ways to Create Objects in Java

Introduction Java comes under the category of object-oriented programming languages. Since it is object-oriented, everything in Java is considered an object. Java is a diverse programming language that is designed to...

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

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 Transient

Java Transient In Java, Serialization is used to convert an object into a stream of the byte. The byte stream consists of the data of the instance as well as the...

3 minutes read.

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.

Date time API in java

Introduction: In this text, we can talk approximately Data time API in java. The java.time, java.util, java.sql, and java.text packages contain classes that represent dates and times. The following classes are...

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

How to Assign Static Value to Date in Java?

In this tutorial, we will learn certain ways to assign a static value to a date in java. We will see the limitation of each method that will lead to...

3 minutes read.

Minimum Number of Taps to Open to Water a Garden in Java

Problem Statement The issue is that a gardener wants to water a (single-dimensional) garden using the fewest possible tap openings. The goal is to determine the minimum necessary taps to be...

8 minutes read.

Java Program to remove duplicate characters in a string

In strings, we can find many characters present one or more times in a string; accessing a particular character is difficult due to repetition. Duplicate characters will present in the...

5 minutes read.