Java.io.PushbackReader Java中的类
Java.io.PushbackReader是一个字符流读取器类,它允许将字符推回流中。
宣言:
public class PushbackReader
extends FilterReader
构造函数:
- PushbackReader(Reader push) :创建一个新的 Pushback Reader – “push” 带有一个字符Pushback 缓冲区。
- PushbackReader(Reader push, int size) :使用特定大小的 Pushback 缓冲区创建一个新的 Pushback Reader。
PushbackReader 类的方法:
- read() : Java.io.PushbackReader.read()读取单个字符。
句法 :public int read() Parameters : ----------- Return : reads single character from the Pushback buffer else, -1 i.e. when end of file is reached. Exception : -> IOException : If I/O error occurs.
- read(char[] carray, int offset, int maxlen) : Java.io.PushbackReader.read(char[] carray, int offset, int maxlen)读取字符数组的某些部分
句法 :public int read(char[] carray, int offset, int maxlen) Parameters : carray : destination buffer to be read into offset : starting position of the carray maxlen : maximum no. of characters to be read Return : reads some portion of the character array else, -1 i.e. when end of file is reached. Exception : -> IOException : If I/O error occurs.
- close() : Java.io.PushbackReader.close()关闭 PushbackReader 并将与此流相关的系统资源释放到垃圾收集器。
句法 :public void close() Parameters : ------ Return : void Exception : -> IOException : If I/O error occurs.
- mark(int arg) : Java.io.PushbackReader.mark(int arg)标记 PushbackReader 的当前位置。对于 PushbackReader,此方法总是抛出异常。
句法 :public void mark(int arg) Parameters : arg : integer specifying the read limit of the Stream Return : void
- skip(long char) : Java.io.PushbackReader.skip(long char)跳过并丢弃'char'号。 PushbackReader 数据中的字符数。
句法 :public long skip(long char) Parameters : char : no. of bytes of PushbackReader data to skip. Return : no. of bytes to be skipped Exception: -> IOException : in case I/O error occurs
- reset() : Java.io.PushbackReader.reset()由 mark() 方法调用。它将 PushbackReader 重新定位到标记的位置。对于 PushbackReader,此方法总是抛出异常。
句法 :public void reset() Parameters : ---- Return : void Exception : -> IOException : If I/O error occurs.
- markSupported() : Java.io.PushbackReader.markSupported()判断此流是否支持 mark() 操作,但它不支持。
句法 :public boolean markSupported() Parameters : ------- Return : true if PushbackReader supports the mark() method else,false
- ready() : Java.io.PushbackReader.ready()告诉流是否准备好被读取
句法 :public boolean ready() Parameters : ------- Return : true if the stream is ready to be read else,false
- unread(int char) : Java.io.PushbackReader.unread(int char)将单个字符推送到 Pushback 缓冲区。在此方法返回后,要读取的下一个字符将具有 (char)char 值。
句法 :
public void unread(int char) Parameters : char : int value of the character to be pushed back Return : void Exception : -> IOException : If I/O error occurs or Pushback buffer is full.
- unread(char[] carray) : Java.io.PushbackReader.unread(char[] carray)将一个字符数组推送到 Pushback 缓冲区。被推送的数组的索引将从 0 开始。
句法 :public void unread(char[] carray) Parameters : carray : character array to be pushed back Return : void Exception : -> IOException : If I/O error occurs or Pushback buffer is full.
解释 PushbackReader 方法的Java代码:read(char[] carray)、close()、markSupported()、read()、mark()、ready()、skip()、unread()
// Java program illustrating the working of PushbackReader // read(char[] carray), close(), markSupported() // read(), mark(), ready(), skip(), unread() import java.io.*; public class NewClass { public static void main(String[] args) throws IOException { try { // Initializing a StringReader and PushbackReader String s = "GeeksForGeeks"; StringReader str_reader = new StringReader(s); PushbackReader geek_pushReader1 = new PushbackReader(str_reader); PushbackReader geek_pushReader2 = new PushbackReader(str_reader); // Use of ready() method : System.out.println("Is stream1 ready : " + geek_pushReader1.ready()); System.out.println("Is stream2 ready : " + geek_pushReader2.ready()); // Use of read() : System.out.println("\nWe have used skip() method in 1 : "); System.out.print("\nUse of read() in 1 : "); for (int i = 0; i < 6; i++) { char c = (char) geek_pushReader1.read(); System.out.print(c); // Use of skip() : geek_pushReader1.skip(1); } System.out.println(""); // USing read() : char[] carray = new char[20]; System.out.println("Using read() in 2 : " + geek_pushReader2.read(carray)); // USe of markSupported() : System.out.println("\nIs mark supported in 1 : " + geek_pushReader1.markSupported()); geek_pushReader2.unread('F'); // read the next char, which is the one we unread char c3 = (char) geek_pushReader2.read(); System.out.println("USe of unread() : " + c3); // USe of mark() : geek_pushReader1.mark(5); // Use of close() : geek_pushReader1.close(); } catch (IOException excpt) { System.out.println("mark not supported in 1"); } } }
输出 :
Is stream1 ready : true Is stream2 ready : true We have used skip() method in 1 : Use of read() in 1 : GesoGe Using read() in 2 : 1 Is mark supported in 1 : false USe of unread() : F mark not supported in 1