当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Reader mark(int)用法及代码示例


一旦调用reset(),Java中Reader类的mark()方法用于将流标记为从开始​​读取流的检查点。 Reader类的所有子类均不支持此方法。

用法:

public void mark(int readAheadLimit)

参数:此方法接受强制性参数readAheadLimit,该参数是在保留标记的同时可以读取的字符数限制。读取了这么多字符后,尝试重置流可能会失败。


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

异常:如果在不支持输入输出或mark()方法时发生某些错误,则此方法将引发IOException。

下面的方法说明了mark()方法的用法方式:

示例1:

// Java program to demonstrate 
// Reader mark() 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 mark() 
            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 mark() 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 
            // 10 characters using mark() 
            reader.mark(10); 
  
            // reset the stream position 
            reader.reset(); 
  
            // Read the 1 characters from marked position 
            // to this reader using read() method 
            for (int i = 0; i < 1; i++) { 
                ch = reader.read(); 
                System.out.print((char)ch); 
            } 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
输出:
GeeksForGe
e

参考: https://docs.oracle.com/javase/9/docs/api/java/io/Reader.html#mark-int-



相关用法


注:本文由纯净天空筛选整理自Code_r大神的英文原创作品 Reader mark(int) method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。