mark长度和缓存区大小中有一个大于文件大小都可以实现返回到mark 位置
实例:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedStreamDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream fInputStream =null ;
FileOutputStream fOutputStream =null ;
BufferedInputStream fBufferedInputStream =null;
BufferedOutputStream fBufferedOutputStream = null;
try {
fInputStream =new FileInputStream("e:/io/a.txt");
fOutputStream =new FileOutputStream("e:/io/b.txt");
// 处理流的构造函数必须以一个流对象做参数
fBufferedInputStream = new BufferedInputStream(fInputStream,5); // 缓冲区默认为32字节 如果发到32字节会自动清空缓冲区
fBufferedOutputStream = new BufferedOutputStream(fOutputStream);
int b;
fBufferedInputStream.mark(15); // mark长度和缓存区大小中有一个大于文件大小都可以实现返回到mark 位置
try {
while((b = fBufferedInputStream.read()) !=-1){
char c = (char) b;
System.out.print(c+" ");
fBufferedOutputStream.write(b);
}
System.out.println();
fBufferedInputStream.reset();
while((b = fBufferedInputStream.read()) !=-1){
char c = (char) b;
System.out.print(c+" ");
}
fBufferedOutputStream.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
// 关闭自动清空缓存区,数据保存
fInputStream.close();
fOutputStream.close();
fBufferedInputStream.close();
fBufferedOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
网友评论