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
raja
Published on 05-Aug-2019 10:12:20
Advertisements