IO缓冲流
字节缓冲输入输出流
创建缓冲区,提高性能
- 字节缓冲输入流构造方法:public BufferedInputStream(InputStream in)
- 字节缓冲输出流构造方法:public BufferedOutputStream(OutputStream out)
import java.io.*;
public class Buffer {
public static void main(String[] args) throws IOException {
// 字节缓冲输入流构造方法:public BufferedInputStream(InputStream in)
// 字节缓冲输出流构造方法:public BufferedOutputStream(OutputStream out)
// new一个FileOutputStream类对象
// FileOutputStream fileOutputStream = new FileOutputStream("java基础\\src\\com\\io\\buffer\\img.jpg");
// BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
// 合并为一步
// BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("java基础\\src\\com\\io\\buffer\\java.txt"));
// 写入数据
// bufferedOutputStream.write("hello\r\n".getBytes());
// bufferedOutputStream.write("world".getBytes());
// new一个FileInputStream类对象
// FileInputStream fileInputStream = new FileInputStream("java基础\\src\\com\\io\\buffer\\img.jpg");
// BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
// 合并为一步
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("java基础\\src\\com\\io\\buffer\\java.txt"));
// 一次读取一个字节数据
// int read;
// while ((read = bufferedInputStream.read()) != -1){
// System.out.print((char)read);
// }
// 一次读取一个字节数组
byte[] bytes = new byte[1024];
int read;
while ((read = bufferedInputStream.read(bytes)) != -1){
System.out.print(new String(bytes,0,read));
}
// 释放资源
// bufferedOutputStream.close();
bufferedInputStream.close();
}
}