Java中BufferedReader類的reset()方法用於將位置固定或標記在最後標記的位置,以便可以再次讀取相同的字節。
用法:
public void reset() throws IOException
覆蓋:覆蓋Reader類的reset()方法。
參數:該方法不接受任何參數。
返回值:該方法不返回任何值。
異常:如果從不在流上調用mark()方法,或者mark的值無效,則該方法將引發IOException。
以下示例程序旨在說明IO包中BufferedReader類中的reset()方法:
程序1:假設存在文件“c:/demo.txt”。
// Java program to illustrate
// BufferedReader reset() method
import java.io.*;
public class GFG {
public static void main(String[] args)
{
// Read the stream 'demo.txt'
// for containing text "GEEKS"
FileReader fileReader
= new FileReader(
"c:/demo.txt");
// Convert fileReader to
// bufferedReader
BufferedReader buffReader
= new BufferedReader(
fileReader);
// Read and print characters
// one by one
System.out.println(
"Char:"
+ (char)buffReader.read());
System.out.println(
"Char:"
+ (char)buffReader.read());
System.out.println(
"Char:"
+ (char)buffReader.read());
// Mark is set on the stream
buffReader.mark(0);
System.out.println(
"Char:"
+ (char)buffReader.read());
// Reset() is invoked
buffReader.reset();
// Read and print characters
System.out.println(
"Char:"
+ (char)buffReader.read());
System.out.println(
"Char:"
+ (char)buffReader.read());
}
}
輸出:
Char:G Char:E Char:E Char:K Char:K Char:S
程序2:假設存在文件“c:/demo.txt”。
// Java program to illustrate
// BufferedReader reset() method
import java.io.*;
public class GFG {
public static void main(String[] args)
{
// Read the stream 'demo.txt'
// containing text "GEEKSFORGEEKS"
FileReader fileReader
= new FileReader(
"c:/demo.txt");
// Convert fileReader to
// bufferedReader
BufferedReader buffReader
= new BufferedReader(
fileReader);
// Read and print characters
// one by one
System.out.println(
"Char:"
+ (char)buffReader.read());
System.out.println(
"Char:"
+ (char)buffReader.read());
System.out.println(
"Char:"
+ (char)buffReader.read());
System.out.println(
"Char:"
+ (char)buffReader.read());
// Mark is set on the stream
buffReader.mark(0);
System.out.println(
"Char:"
+ (char)buffReader.read());
// Reset() is invoked
buffReader.reset();
// read and print characters
System.out.println(
"Char:"
+ (char)buffReader.read());
System.out.println(
"Char:"
+ (char)buffReader.read());
System.out.println(
"Char:"
+ (char)buffReader.read());
System.out.println(
"Char:"
+ (char)buffReader.read());
}
}
輸出:
Char:G Char:E Char:E Char:K Char:S Char:S Char:F Char:O Char:R
參考文獻:
https://docs.oracle.com/javase/10/docs/api/java/io/BufferedReader.html#reset()
相關用法
- Java BufferedReader markSupported()用法及代碼示例
- Java BufferedReader mark()用法及代碼示例
- Java BufferedReader readLine()用法及代碼示例
- Java BufferedReader ready()用法及代碼示例
- Java BufferedReader read()用法及代碼示例
- Java BufferedReader close()用法及代碼示例
- Java BufferedReader skip(long)用法及代碼示例
- Java CharArrayWriter reset()用法及代碼示例
- Java Scanner reset()用法及代碼示例
- Java StringReader reset()用法及代碼示例
- Java LongAdder reset()用法及代碼示例
- Java PushbackReader reset()用法及代碼示例
- Java Reader reset()用法及代碼示例
- Java CollationElementIterator reset()用法及代碼示例
- Java BufferedInputStream reset()用法及代碼示例
- Java MessageDigest reset()用法及代碼示例
- Java Matcher reset()用法及代碼示例
- Java DoubleAdder reset()用法及代碼示例
- Java LogManager reset()用法及代碼示例
- Java DoubleAccumulator reset()用法及代碼示例
注:本文由純淨天空篩選整理自pp_pankaj大神的英文原創作品 BufferedReader reset() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。