StringReader is extending from the Reader class. If you have string and wants to convert that to a Reader object, then StringReader is useful. It is a specialized version for using the Reader with strings. If you would like to read the Files, then choose FileReader instead of StringReader.
This class has only one constructor which takes the String object as the parameter. Lets look at the example to understand how to use the StringReader class.
StringReaderExample.java
package javabeat.net.core; import java.io.IOException; import java.io.StringReader; /** * Java StringReader Example * * @author Krishna * */ public class StringReaderExample { /** * @param args */ public static void main(String[] args) throws IOException { String str = "StringReader Example Program!!"; //Create StringReader instance StringReader reader = new StringReader(str); int c = reader.read(); while (c != -1){ //Converting to character System.out.print((char)c); c= reader.read(); } //Closing the file io reader.close(); } }
Output…
StringReader Example Program!!