java.io.PushbackReader 是一个字符流读取器类,它允许将字符推回流中。
声明:
public class PushbackReader extends FilterReader
构造函数:
- PushbackReader(阅读器推送):创建一个新的推回读取器 - “push” 并带有字符推回缓冲区。
- PushbackReader(读卡器推送,int大小):创建一个具有特定大小的推回缓冲区的新推回读取器。
PushbackReader类的方法:
- read():java.io.PushbackReader.read()读取单个字符。
Syntax :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.
- 读取(char[] carray,int 偏移量,int maxlen):java.io.PushbackReader.read(char[] carray, int offset, int maxlen)读取字符数组的某些部分
Syntax :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 并将与此流关联的系统资源释放到垃圾Collector。
Syntax :public void close() Parameters : ------ Return : void Exception : -> IOException : If I/O error occurs.
- 标记(int arg):java.io.PushbackReader.mark(int arg)标记 PushbackReader 的当前位置。对于 PushbackReader,此方法总是抛出异常。
Syntax :public void mark(int arg) Parameters : arg : integer specifying the read limit of the Stream Return : void
- 跳过(长字符):java.io.PushbackReader.skip(长字符)跳过并丢弃‘char’号。 PushbackReader 数据中的字符数。
Syntax :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,此方法总是抛出异常。
Syntax :public void reset() Parameters : ---- Return : void Exception : -> IOException : If I/O error occurs.
- markSupported():java.io.PushbackReader.markSupported()告知此流是否支持 mark() 操作,但它不支持。
Syntax :public boolean markSupported() Parameters : ------- Return : true if PushbackReader supports the mark() method else,false
- ready():java.io.PushbackReader.ready()告知流是否已准备好读取
Syntax :public boolean ready() Parameters : ------- Return : true if the stream is ready to be read else,false
- 未读(int char):java.io.PushbackReader.unread(int char)将单个字符推入推回缓冲区。在此方法返回后,要读取的下一个字符将具有 (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.
- 未读(char[] carray):java.io.PushbackReader.unread(char[] carray)将字符数组推送到 Pushback 缓冲区。被压入的数组的索引从 0 开始。
Syntax :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
相关用法
- Java Java.io.PushbackReader.close()用法及代码示例
- Java Java.io.PushbackReader.mark()用法及代码示例
- Java Java.io.PushbackReader.markSupported()用法及代码示例
- Java Java.io.PushbackReader.read()用法及代码示例
- Java Java.io.PushbackReader.ready()用法及代码示例
- Java Java.io.PushbackReader.reset()用法及代码示例
- Java Java.io.PushbackReader.skip()用法及代码示例
- Java Java.io.PushbackReader.unread()用法及代码示例
- Java Java.io.PushbackInputStream.available()用法及代码示例
- Java Java.io.PushbackInputStream.close()用法及代码示例
- Java Java.io.PushbackInputStream.mark()用法及代码示例
- Java Java.io.PushbackInputStream.read()用法及代码示例
- Java Java.io.PushbackInputStream.reset()用法及代码示例
- Java Java.io.PushbackInputStream.skip()用法及代码示例
- Java Java.io.PushbackInputStream.unread()用法及代码示例
- Java Java.io.PushbackInputStream markSupported()用法及代码示例
- Java Java.io.PushbackInputStream用法及代码示例
- Java Java.io.PipedInputStream.available()用法及代码示例
- Java Java.io.PipedInputStream.close()用法及代码示例
- Java Java.io.PipedInputStream.connect()用法及代码示例
- Java Java.io.PipedInputStream.read()用法及代码示例
- Java Java.io.PipedOutputStream.close()用法及代码示例
- Java Java.io.PipedOutputStream.connect()用法及代码示例
- Java Java.io.PipedOutputStream.flush()用法及代码示例
- Java Java.io.PipedOutputStream.write()用法及代码示例
注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Java.io.PushbackReader Class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。