📜  Java I/O-StringReader类

📅  最后修改于: 2020-09-27 07:27:15             🧑  作者: Mango

Java StringReader类

Java StringReader类是一个以字符串为源的字符流。它接受输入字符串并将其更改为字符流。它继承了Reader类。

在StringReader类中,未使用系统资源(如网络套接字和文件),因此不需要关闭StringReader。

Java StringReader类声明

让我们看一下Java.io.StringReader类的声明:

public class StringReader extends Reader

StringReader类的方法

Method Description
int read() It is used to read a single character.
int read(char[] cbuf, int off, int len) It is used to read a character into a portion of an array.
boolean ready() It is used to tell whether the stream is ready to be read.
boolean markSupported() It is used to tell whether the stream support mark() operation.
long skip(long ns) It is used to skip the specified number of character in a stream
void mark(int readAheadLimit) It is used to mark the mark the present position in a stream.
void reset() It is used to reset the stream.
void close() It is used to close the stream.

Java StringReader示例

import java.io.StringReader;
public class StringReaderExample {
public static void main(String[] args) throws Exception {
        String srg = "Hello Java!! \nWelcome to Javatpoint.";
        StringReader reader = new StringReader(srg);
        int k=0;
while((k=reader.read())!=-1){
System.out.print((char)k);
}
        }
}

输出:

Hello Java!! 
Welcome to Javatpoint.