當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Reader reset()用法及代碼示例


Java中Reader類的reset()方法用於重置流。重置後,如果流已被標記,則此方法嘗試將其重新定位在標記處,否則它將嘗試將其定位到起點。

用法:

public void reset()

參數:此方法不接受任何參數。


返回值:此方法不返回任何值。

異常:此方法引發IOException:

  • 如果輸入輸出時發生一些錯誤
  • 如果尚未標記流
  • 商標是否無效
  • 如果不支持reset()方法。

下麵的方法說明了reset()方法的用法方式:

示例1:

// Java program to demonstrate 
// Reader reset() method 
  
import java.io.*; 
import java.util.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        try { 
  
            String str = "GeeksForGeeks"; 
  
            // Create a Reader instance 
            Reader reader 
                = new StringReader(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 
// Reader reset() method 
  
import java.io.*; 
import java.util.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        try { 
            String str = "GeeksForGeeks"; 
  
            // Create a Reader instance 
            Reader reader 
                = new StringReader(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/Reader.html#reset–



相關用法


注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 Reader reset() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。