Java.io - SPLessons

Java.io StringReader

Home > Lesson > Chapter 50
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io StringReader

Java.io StringReader

shape Introduction

The Java.io StringReader is the class used to read the character stream in the form of string which acts as a source to Java.io StringReader. The string reader is a subclass to the reader class, it overrides all the methods from reader class.

Class Declaration

shape Declaration

Java.io StringReader class is declared below: Public class StringReader extends Reader

shape Fields

Java.io StringReader class fields are:
  • Protected Object Lock : For synchronize operations this object is used in StringReader.

Class Constructors

shape Table

Constructor Description
String Reader(String s) The function of this constructor is to create the string reader instance and used to read from the character stream.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to this stream, systems resources are released without any impact.
Long skip (long m) The function of this method is to skip the text or characters in this stream of ' m ' characters.
Boolean markSupported () The function of this method is to check the represented mark method is supported by this stream or not
int read(Char[] cbuf, int off, int n) By using this method it reads the data up to 'n' bytes from the character stream into an array of characters.

Inherited Methods

shape Description

From the following classes, methods are inherited to the string reader class.
  • Java.io.object
  • Java.io.Reader

shape Examples

Following is an example.. [c]package com.cp.io; import java.io.IOException; import java.io.StringReader; public class StringReaderDemo { public static void main(String[] args) { String str = "Hello World! \nThis is StringReader Program."; StringReader sr = new StringReader(str); int i=0; try { while((i=sr.read())!=-1){ System.out.print((char)i); } } catch (IOException e) { e.printStackTrace(); } } } [/c] Output The result will be as follows. [java]Hello World! This is StringReader Program.[/java]

Summary

shape Key Points

  • The string reader is the class used to read the character stream in the form of string which acts as a source to string reader.
  • The string reader is a subclass to the reader class, it overrides all the methods from reader class.