×

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

How to download Eclipse for Java

Introduction: We write a java program, and when we want to run it, we need software to run it. Eclipse is a kind of software used to execute a JavaFX...

3 minutes read.

Bully Algorithm code in Java

Election algorithms include the bully algorithm, mainly used to select a coordinate. To find a coordinator in a distributed system that can carry out the tasks required by other processes,...

4 minutes read.

Math fma () method in java

In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given...

4 minutes read.

Unicode System in Java

In this tutorial, we will understand the meaning and emergence of the Unicode system in java. The Unicode system is one of the important and useful features in the world...

6 minutes read.

Java Localization

Internationalization is the process of creating a software application that can be translated into different languages and regions without modifying the application. Creating a locale-specific application raises the cost of...

3 minutes read.

Java Null Keyword

Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's...

3 minutes read.

Java String vs StringBuffer

Java String vs StringBuffer In this section, we will discuss the key differences between String and StringBuffer class. Before moving to the ahead in this section, let’s introduce with both classes. String...

4 minutes read.

Java Boolean toString() Method

The toString() method of Java Boolean class returns a String corresponding to this Boolean object. It returns a string value “true”, if the defined object is true else it returns...

2 minutes read.

Implementing Queue Using Array in Java

We can implement queue by using array in Java. The queue is a linear data structure, and the array is one of the simplest data structures. Before implementing a queue...

4 minutes read.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.

Java Characters

Normally, when we work with characters, we use primitive data types char. When we have to work with the objects of char, we use Character class. Character class has many important...

2 minutes read.

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

Access specifiers in Java

What are access specifiers in Java? Access specifiers in Java are used to determine whether other classes can use a particular field or invoke a particular method. Access specifiers mainly specify...

7 minutes read.

How to Convert long to int in Java

How to Convert long to int in Java When we assign a larger type value to a variable of smaller type, then we need to perform explicit casting for the conversion....

2 minutes read.

Java Boolean logicalAnd() Method

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

2 minutes read.

Java Integer floatValue() method

The floatValue() method of Integer class returns a float value for this Integer after a widening primitive conversion. Syntax public float floatValue() Parameters NA Specified by This method is specified by floatValue in class Number Return Value This...

1 minute read.

Java Tokens

Classes and methods are included in the Java program. The procedures also provide the expressions and statements required to finish a specific operation. Tokens make up the sentences and expressions...

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.

Ad Hoc Problem on Arrays in Java

Ad hoc problems are issues that arise unexpectedly and require immediate attention. These problems can range from small and straightforward issues to more complex and time-consuming problems. Examples of ad...

3 minutes read.

Java Integer decode() method

The decode() method of Integer class decodes a String into an Integer. It can accept decimal, hexadecimal and octal numbers. Syntax public static Integer decode(String nm) throws NumberFormatException Parameters The parameter ‘nm’ represents the...

2 minutes read.