字节缓冲流
package com.itheima.d2;
import java.io.*;
public class Test1 {
public static void main(String[] args) {
try (
InputStream is = new FileInputStream("IO/src/itheima01.txt");
//1、定义一个字节缓冲输入流包装原始的字节输入流
InputStream bis = new BufferedInputStream(is, 8192 * 2);
OutputStream os = new FileOutputStream("IO/src/itheima02.txt");
//2、定义一个字节缓冲输出流包装原始的字节输出流
OutputStream bos = new BufferedOutputStream(os, 8192 * 2);
){
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1){
bos.write(buffer, 0, len);
}
System.out.println("复制完成");
} catch (Exception e) {
e.printStackTrace();
}
}
}
字符缓冲流
BufferedWriter(字符缓冲输入流)
package com.itheima.d2;
import java.io.FileReader;
import java.io.Reader;
public class Test2 {
public static void main(String[] args) {
try(
Reader fr = new FileReader("IO\\src\\itheima04.txt");
//创建一个字符缓冲输入流包装原始的字符输入流
BufferedReader br = new BufferedReader(fr)
){
String line;//记住每次读取的一行数据
while((line = br.readLine()) != null){
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
BufferedWriter(字符缓冲输出流)
package com.itheima.d2;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Writer;
public class Test3 {
public static void main(String[] args) {
try(
Writer fw = new FileWriter("IO/src/itheima05.txt");
//创建一个字符缓冲输出流管道包装原始的字符输出流
BufferedWriter bw = new BufferedWriter(fw);
) {
bw.write('a');
bw.newLine();
bw.write('c');
}catch (Exception e){
e.printStackTrace();
}
}
}
原始流、缓冲流的性能分析【重点】
字节数组越大越好,但大到一定程度对性能的影响微乎其微