📜  Java中的 StringReader mark(int) 方法及示例

📅  最后修改于: 2022-05-13 01:55:20.261000             🧑  作者: Mango

Java中的 StringReader mark(int) 方法及示例

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

句法:

public void mark(int readAheadLimit)

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

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

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

以下方法说明了 mark() 方法的工作原理:

方案一:

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

方案二:

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