คลาส StringReader เป็น subclass ของ Reader และสามารถใช้เพื่ออ่าน character stream ในรูปแบบ string ซึ่งทำหน้าที่เป็น source ให้กับ StringReader คลาส StringReader ทับทอด (override) เมธอดทั้งหมดจากคลาส Reader เมธอดสำคัญของ StringReader ได้แก่ skip(), close(), mark(), markSupported(), reset() เป็นต้น
Syntax
public class StringReader extends Reader
ตัวอย่าง
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