Java中CharArrayReader类的reset()方法用于重置流。重置后,如果流已被标记,则此方法尝试将其重新定位在标记处,否则它将尝试将其定位到起点。
用法:
public void reset()
参数:此方法不接受任何参数。
返回值:此方法不返回任何值。
异常:此方法引发IOException:
- 如果输入输出时发生一些错误
- 如果尚未标记流
- 商标是否无效
- 如果不支持reset()方法。
下面的方法说明了reset()方法的用法方式:
示例1:
// Java program to demonstrate
// CharArrayReader reset() method
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
try {
char[] str = { 'G', 'e', 'e', 'k', 's',
'F', 'o', 'r',
'G', 'e', 'e', 'k', 's' };
// Create a CharArrayReader instance
CharArrayReader reader
= new CharArrayReader(str);
// Get the character
// to be read from the stream
int ch;
// Read the first 10 characters
// to this reader using read() method
// This will put the str in the stream
// till it is read by the reader
for (int i = 0; i < 10; i++) {
ch = reader.read();
System.out.print((char)ch);
}
System.out.println();
// mark the stream for
// 5 characters using reset()
reader.mark(5);
// reset the stream position
reader.reset();
// Read the 5 characters from marked position
// to this reader using read() method
for (int i = 0; i < 5; i++) {
ch = reader.read();
System.out.print((char)ch);
}
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出:
GeeksForGe eks??
示例2:
// Java program to demonstrate
// CharArrayReader reset() method
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
try {
char[] str = { 'G', 'e', 'e', 'k', 's',
'F', 'o', 'r',
'G', 'e', 'e', 'k', 's' };
// Create a CharArrayReader instance
CharArrayReader reader
= new CharArrayReader(str);
// Get the character
// to be read from the stream
int ch;
// Read the first 10 characters
// to this reader using read() method
// This will put the str in the stream
// till it is read by the reader
for (int i = 0; i < 10; i++) {
ch = reader.read();
System.out.print((char)ch);
}
System.out.println();
// reset the stream position
reader.reset();
// Read the 5 characters from marked position
// to this reader using read() method
for (int i = 0; i < 5; i++) {
ch = reader.read();
System.out.print((char)ch);
}
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出:
GeeksForGe Geeks
参考: https://docs.oracle.com/javase/9/docs/api/java/io/CharArrayReader.html#reset–
相关用法
- Java CharArrayReader read()用法及代码示例
- Java CharArrayReader markSupported()用法及代码示例
- Java CharArrayReader mark(int)用法及代码示例
- Java CharArrayReader close()用法及代码示例
- Java CharArrayReader ready()用法及代码示例
- Java CharArrayReader read(char[], int, int)用法及代码示例
- Java CharArrayReader read(CharBuffer)用法及代码示例
- Java CharArrayReader read(char[])用法及代码示例
- Java CharArrayReader skip(long)用法及代码示例
- Java LogManager reset()用法及代码示例
- Java CharsetEncoder reset()用法及代码示例
- Java DoubleAdder reset()用法及代码示例
- Java PushbackReader reset()用法及代码示例
- Java Matcher reset()用法及代码示例
- Java ByteArrayInputStream reset()用法及代码示例
注:本文由纯净天空筛选整理自Code_r大神的英文原创作品 CharArrayReader reset() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。