reset()
方法reset()方法在java.io包中可用。
reset()方法用于重置此LineNumberInputStream,并将流重置为该流上最近mark()
设置的位置。
reset()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
reset()方法在重置流时可能会引发异常。
IOException:在执行过程中遇到任何输入/输出错误时,可能引发此异常。
语法:
public void reset();
参数:
它不接受任何参数。
返回值:
该方法的返回类型为void,不返回任何内容。
示例
//Java程序演示示例 // 标记(int r_limit)方法 //LineNumberInputStream- import java.io.*; public class MarkOfLNIS { public static void main(String[] args) throws Exception { FileInputStream fis_stm = null; LineNumberInputStream line_stm = null; int val = 0; try { //实例化FileInputStream- fis_stm = new FileInputStream("D:\\includehelp.txt"); line_stm = new LineNumberInputStream(fis_stm); //通过使用read()方法isto- //从line_stm中读取字符 char ch1 = (char) line_stm.read(); char ch2 = (char) line_stm.read(); char ch3 = (char) line_stm.read(); System.out.println("ch1: " + ch1); System.out.println("ch2: " + ch2); System.out.println("ch3: " + ch3); //通过使用mark()方法isto- //在此设置当前位置 //line_stm- System.out.println("line_stm.mark(0): "); line_stm.mark(0); char ch4 = (char) line_stm.read(); char ch5 = (char) line_stm.read(); System.out.println("ch4: " + ch4); System.out.println("ch5: " + ch5); //通过使用reset()方法isto- //将流重置到位置 //通过调用mark()方法设置 System.out.println("line_stm.reset(): "); line_stm.reset(); char ch6 = (char) line_stm.read(); char ch7 = (char) line_stm.read(); char ch8 = (char) line_stm.read(); char ch9 = (char) line_stm.read(); System.out.println("ch4: " + ch6); System.out.println("ch5: " + ch7); System.out.println("ch6: " + ch8); System.out.println("ch7: " + ch9); } catch (Exception ex) { System.out.println(ex.toString()); } finally { //借助此块可以 //释放所有链接的必要资源 //与流 if (fis_stm != null) { fis_stm.close(); if (line_stm != null) { line_stm.close(); } } } } }
输出结果
ch1: J ch2: A ch3: V line_stm.mark(0): ch4: A ch5: W line_stm.reset(): Reset Not Supported