×

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 Enhanced For Loop (For-each Loop)

JAVA ENHANCED FOR LOOP The enhanced for loop is also called the for-each loop and has some advantages over the regular for loop. A for-each loop is designed to cycle through...

4 minutes read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.

Perfect Number Program in Java

Perfect Number Program in Java A perfect number is a number whose sum of all the factors, excluding the number itself, is equal to the number. For example, 28 is a...

4 minutes read.

JRE (Java Runtime Environment)

JRE is an installation package that provides an environment to run the Java program on any Operating System. It does not deal with the development process of any application. It is a part...

2 minutes read.

Resultset in java

Resultset: A result set is an interface that is present in the package java.sql and the resultset is used to store the data that are returned from the database table after...

5 minutes read.

Hello Program in Java

Hello Program in Java The Hello program in Java displays the word Hello on the console. It is the basic program in Java. In this section, we will learn different ways...

3 minutes read.

CRC Program in Java

The acronym CRC stands for Cyclic Redundancy Check. It is invented by W. Wesley Peterson in 1961. It is an error detection technique that detects errors in digital networks (also...

5 minutes read.

Bounded buffer problem in Java

The Bounded buffer Problem can also be called a Producer consumer problem. The problem covers two processes—the producer and the consumer—that share a single, fixed-size buffer that serves as a...

4 minutes read.

Java Integer getInteger() method

The getInteger() method of Integer class determines the integer value of the system property with the given name. Syntax` public static Integer getInteger(String nm) Parameters The parameter ‘nm’ represents the property name. Throws The getInteger ()...

1 minute read.

URLConnection Class

What is the URL? URL stands for Uniform Resource Locator, is used to specify addresses on the World Wide Web. A URL relates to the identification of any resource connected to the web. URL syntax: Protocol://hostname/other_information(files...

6 minutes read.

Java Binary Operators

In this article, we are going to discuss about the binary operators in Java and their operations. Also, an example program will be discussed along with the operations. These are...

6 minutes read.

Java Short Keyword

Java supports eight different primitive datatypes. The language has predefined primitive datatypes that are given keyword names. Let's take a closer look at each of the eight primitive data types....

3 minutes read.

Java logger

Logging is a crucial Java feature that aids developers in identifying issues. The logging methodology is included with Java as the programming language. It offers the Logging API, which debuted...

7 minutes read.

Java RandomAccessfile

Writing and reading to random access files are done using this class. An array of many bytes is how a random access file operates. By changing the implied file pointer...

3 minutes read.

Heap Implementation in Java

The root node or parent node of a Java heap is compared to its left and right offspring, and the children are then ordered in line with the comparison.Assuming that...

9 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.

Economical number in Java

Economical number is said to be economically efficient if the number of digits after prime factorization of the original number with their powers is less than the number of digits...

3 minutes read.

Java Math negateExact() Method

The negateExact() method of Math class returns the negation for the specified argument, throwing an exception if the result overflows an int or a long. Syntax: public static int negateExact (int a)public...

1 minute read.

Buffer reader to read string in Java

The Buffered Reader class of Java is used to read the stream of characters from the input stream. Program to read string using Buffer reader import java.io.*; class  Demo {   public static void main(String...

3 minutes read.