BufferedInputStream的mark和reset。

/**
 * FileName:FileInputStreamException.java
 * @author zhanggw
 * @date 2018年2月3日 下午1:19:01
 */
package fkjava;

import java.io.BufferedInputStream;
import java.io.FileInputStream;

/**
 * @ClassName FileInputStreamException
 * @author zhanggw
 * @date 2018年2月3日 下午1:19:01
 */
public class FileInputStreamExceptionDemo {
    public static void main(String[] args) throws Exception{
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("d://temp/MethodParameterTest.java"));
       
        // 开始读取内容
        int hasRead = 0;
        byte[] buff = new byte[64];
        int readTimes = 0;
        while((hasRead=bufferedInputStream.read(buff))!=-1){
            System.out.println(new String(buff,0,hasRead));
            if(readTimes==1){
                System.out.println("开始标记");
                bufferedInputStream.mark(bufferedInputStream.available()+1);
            }
            readTimes++;
        }
        
        // 回退重读
        System.out.println("开始回退重读");
        bufferedInputStream.reset();
        byte[] newBuff = new byte[64];
        int newLen = 0;
        while((newLen=bufferedInputStream.read(newBuff))!=-1){
            System.out.println(new String(newBuff,0,newLen));
        }
        
        bufferedInputStream.close();
    }
}

猜你喜欢

转载自blog.csdn.net/leadseczgw01/article/details/79246990