Java Reader mark(int)方法及示例

Java Reader mark(int)方法及示例

Java中Reader类的mark()方法是用来标记流的检查点,一旦调用reset(),流的读取将从这里开始。不是所有的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-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程