【Java寒假打卡】Java基础-字节缓冲输入和输出流
概述
- 字节缓冲输入流是指:将源文件中的数据输入到缓冲区中
- 字节缓冲输出流是指:将缓冲区内容输出到目标文件中
package com.hfut.edu.test9;
import java.io.*;
public class test16 {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\heima\\1.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\heima\\2.txt")));
int b;
while((b = bis.read()) != -1){
bos.write(b);
}
bis.close();
bos.close();
}
}
字节缓冲流的原理
缓冲流一次读取一个字节数组
package com.hfut.edu.test9;
import java.io.*;
public class test16 {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\heima\\1.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\heima\\2.txt")));
int len;
byte[] bytes = new byte[1024];
while((len = bis.read(bytes)) != -1){
bos.write(bytes,0,len);
}
bis.close();
bos.close();
}
}