mark()
方法mark()方法在java.io包中可用。
mark()方法用于标记此FilterReader流中的当前位置。
mark()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
mark()方法在标记流时可能会引发异常。
IOException:如果给定参数无效,则可能引发此异常。
语法:
public void mark(int r_limit);
参数:
int r_limit –表示保留标记时可以读取的字符数限制。
返回值:
该方法的返回类型为void,不返回任何内容。
示例
//Java程序演示示例 //FilterReader 标记(int r_limit)方法的说明 import java.io.*; public class MarkOfFR { public static void main(String[] args) throws Exception { Reader r_stm = null; FilterReader fr_stm = null; try { //实例化StringReader和 //FilterReader- r_stm = new StringReader("JavaWorld!!!!"); fr_stm = new FilterReader(r_stm) {}; //通过使用read()方法isto- //从fr_stm读取字符 char ch1 = (char) fr_stm.read(); char ch2 = (char) fr_stm.read(); char ch3 = (char) fr_stm.read(); System.out.println("ch1: " + ch1); System.out.println("ch2: " + ch2); System.out.println("ch3: " + ch3); //通过使用mark()方法isto- //在此设置当前位置 //fr_stm- System.out.println("fr_stm.mark(1): "); fr_stm.mark(1); char ch4 = (char) fr_stm.read(); char ch5 = (char) fr_stm.read(); System.out.println("ch4: " + ch4); System.out.println("ch5: " + ch5); //通过使用reset()方法isto- //将流重置到位置 //通过调用mark()方法设置 System.out.println("fr_stm.reset(): "); fr_stm.reset(); char ch6 = (char) fr_stm.read(); char ch7 = (char) fr_stm.read(); char ch8 = (char) fr_stm.read(); char ch9 = (char) fr_stm.read(); char ch10 = (char) fr_stm.read(); char ch11 = (char) fr_stm.read(); System.out.println("ch4: " + ch6); System.out.println("ch5: " + ch7); System.out.println("ch6: " + ch8); System.out.println("ch7: " + ch9); System.out.println("ch8: " + ch10); System.out.println("ch9: " + ch11); } catch (Exception ex) { System.out.println(ex.toString()); } finally { //借助此块可以 //释放所有链接的必要资源 //与流 if (fr_stm != null) { fr_stm.close(); } } } }
输出结果
ch1: J ch2: a ch3: v fr_stm.mark(1): ch4: a ch5: W fr_stm.reset(): ch4: a ch5: W ch6: o ch7: r ch8: l ch9: d