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