Importance of StringReader class in Java?


The StringReader class is s subclass of a Reader class and it can be used to read the character stream in the form of a string which acts as a source to StringReader. The StringReader class overrides all the methods from a Reader class. The important methods of StringReader class are skip(), close(), mark(), markSupported(), reset() and etc.

Syntax

Public class StringReader extends Reader

Example

import java.io.StringReader;
import java.io.IOException;
public class StringReaderTest {
   public static void main(String[] args) {
      String str = "Welcome to Tutorials Point";
      StringReader strReader = new StringReader(str);
      try {
         int i;
         while((i=strReader.read()) != -1) {
            System.out.print((char)i);
         }
      } catch(IOException ioe) {
         System.out.println(ioe);
      } finally {
         if(strReader != null) {
            try {
               strReader.close();
            } catch(Exception e) {
               System.out.println(e);
            }
         }
      }
   }
}

Output

Welcome to Tutorials Point

Updated on: 23-Nov-2023

146 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements