Lớp StringReader là một lớp con của Reader và có thể được sử dụng để đọc luồng ký tự dưới dạng chuỗi, đóng vai trò là nguồn cho StringReader. Lớp StringReader ghi đè tất cả các phương thức từ lớp Reader. Các phương thức quan trọng của StringReader bao gồm skip(), close(), mark(), markSupported(), reset() v.v.
Cú pháp
public class StringReader extends Reader
Ví dụ
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);
}
}
}
}
}
Kết quả
Welcome to Tutorials Point