×

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

Java Math toIntExact() Method

The toIntExact() method of Java Math class returns the int value of the given long argument, throwing an exception if the value overflows an int. Syntax: public static int toIntExact (long value) Parameters: The...

2 minutes read.

Default Virtual Behaviour in C++ vs Java

Virtual Behaviour in C++: The class member methods in C++ are, by default, non-virtual. This implies that by simply defining it, they can be turned into virtual. The virtual class can be...

3 minutes read.

Package naming convention in Java

It is conceivable that many programmers will use the same name for various types, given that Java programmers from all over the world create classes and interfaces. For illustration, suppose...

4 minutes read.

Armstrong Number Program in Java

Armstrong Number Program in Java: A positive number is called an Armstrong number if the sum of the cube of each digit is equal to the number itself. There are...

6 minutes read.

3N+1 problem program in Java

The 3N+1 problem is a hypothesis in the field of abstract mathematics (not yet proven). Collectively known as the Collatz problem. This tutorial will describe about the 3N+1 problem and...

3 minutes read.

Java Integer divideUnsigned() method

The divideUnsigned() method of Integer class returns the unsigned quotient by dividing the first argument by the second argument. Syntax public static int divideUnsigned (int dividend , int divisor) Parameters The parameter ‘dividend’ represents...

1 minute read.

Java Pass-by-Reference

In Java, passing parameters can be done using one of two fundamental methods. Pass-by-value is used for the first and pass-by-reference is used for the second. One thing to keep...

2 minutes read.

Interface Program in Java

Interface Program in Java In the previous topic, we discussed that abstraction is possible through the interface and abstract class. An abstract class provides partial to 100% abstraction. 100 %abstraction in...

4 minutes read.

Graphics Program in Java

Graphics Program in Java The graphics program in Java is part of the Swing program in Java. In this section, we will learn about the implementation of custom graphics using some...

6 minutes read.

SHA Decrypt in Java

In this section, we will be acknowledged about the decryption of SHA in Java. Java SHA The SHA cryptographic hash algorithm in cryptography outputs the hash value as an approximately 40-digit-long hexadecimal...

4 minutes read.

Java Iterator

When iterating through, traversing, or retrieving the individual elements of a Collection or Flow object, a Java Cursor is leveraged as an iterator. In Java, there exist three types of...

4 minutes read.

Applet Program in Java

Applet Program in Java An applet is a program that can be embedded in a web page. Applets programs are run by a web browser. It mainly works on the client-side....

3 minutes read.

Java Boolean Keyword

Boolean keyword In java programming language we basically have two types of primitive data types, Boolean and Numeric (integer and floating-point data types). In this article we are going to learn...

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

Null Pointer Exception in Java

It is a runtime error exception. The null value is allocated to the object reference in this exception. We will explicitly throw this null pointer exception when the program wants...

3 minutes read.

Java vs Node.js

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

4 minutes read.

Tug of War in Java

As in the tug-of-war issue, we must divide the given collection of n numbers into two groups of sizes that are equal or nearly equivalent. A minimum difference must exist...

5 minutes read.

Dining Philosophers problem in Java

The Problem of the Dining Philosophers illustrates a concurrency issue involving the distribution of scarce resources among conflicting processes. Problem Statement Imagine a dining table with a circle in the middle and five...

3 minutes read.

Aggregation in Java

Aggregation A "has-a" and "whole/part" connection is the best way to define the aggregate relationship in Java, which exists between two classes. This connection relationship has a higher level of specialisation....

4 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.