Java FilterInputStream reset()方法

java.io.FilterInputStream.reset() 用于重置输入流。

1 语法

public void reset()

2 参数

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.FilterInputStream.reset()方法的例子
 */
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Demo {
    public static void main(String[] args) throws Exception {
        InputStream is = null;
        FilterInputStream fis = null;

        try {
            // create input streams
            is = new FileInputStream("d://test.txt");
            fis = new BufferedInputStream(is);

            // reads and prints filter input stream
            System.out.println((char)fis.read());
            System.out.println((char)fis.read());

            // mark invoked at this position
            fis.mark(0);
            System.out.println("mark() invoked");
            System.out.println((char)fis.read());
            System.out.println((char)fis.read());

            // reset() repositioned the stream to the mark
            fis.reset();
            System.out.println("reset() invoked");
            System.out.println((char)fis.read());
            System.out.println((char)fis.read());

        } catch(IOException e) {
            // if any I/O error occurs
            e.printStackTrace();
        } finally {
            // releases any system resources associated with the stream
            if(is!=null)
                is.close();
            if(fis!=null)
                fis.close();
        }
    }
}

假设test.txt内容如下:

ABCDEF

输出结果为:

A
B
mark() invoked
C
D
reset() invoked
C
D

热门文章

优秀文章