null
JAVA伊奥。推送阅读器 是一个字符流读取器类,允许将字符推回流中。
宣言:
public class PushbackReader extends FilterReader
施工人员:
- PushbackReader(读取器推送): 创建一个新的Pushback读取器—“push”带有字符Pushback缓冲区。
- 回推读取器(读取器推送,整数大小): 使用特定大小的回推缓冲区创建新的回推读取器。
PushbackReader类的方法:
- read(): JAVA伊奥。推送阅读器。读() 读一个字符。 语法:
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.
- 读取(字符[]卡雷,整数偏移,整数最大): JAVA伊奥。推送阅读器。读取(字符[]卡雷,整数偏移,整数最大) 读取字符数组的某些部分 语法:
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.
- 关闭(): JAVA伊奥。推送阅读器。关闭() 关闭PushbackReader并将与此流关联的系统资源释放到垃圾收集器。 语法:
public void close() Parameters : ------ Return : void Exception : -> IOException : If I/O error occurs.
- 标记(int arg): JAVA伊奥。推送阅读器。标记(int arg) 标记回推读取器的当前位置。对于PushbackReader,此方法始终引发异常。 语法:
public void mark(int arg) Parameters : arg : integer specifying the read limit of the Stream Return : void
- 跳过(长字符): JAVA伊奥。推送阅读器。跳过(长字符) 跳过并丢弃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
- 重置(): JAVA伊奥。推送阅读器。重置() 由mark()方法调用。它将回推读取器重新定位到标记位置。对于PushbackReader,此方法总是引发异常。 语法:
public void reset() Parameters : ---- Return : void Exception : -> IOException : If I/O error occurs.
- markSupported(): JAVA伊奥。推送阅读器。markSupported() 说明此流是否支持mark()操作,而它不支持。 语法:
public boolean markSupported() Parameters : ------- Return : true if PushbackReader supports the mark() method else,false
- 就绪(): JAVA伊奥。推送阅读器。就绪() 指示流是否已准备好读取 语法:
public boolean ready() Parameters : ------- Return : true if the stream is ready to be read else,false
- 未读(整型字符): JAVA伊奥。推送阅读器。未读(整型字符) 将单个字符推送到后推缓冲区。此方法返回后,下一个要读取的字符将具有(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伊奥。推送阅读器。未读(char[]carray) 将字符数组推送到后推缓冲区。正在推送的数组的索引将从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(
"We have used skip() method in 1 : "
);
System.out.print(
"Use 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(
"Is 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
本文由 莫希特·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END