1 package day9.lesson1;
2
3 import java.io.*;
4
5 /*
6 1 字节缓冲流
7
8 1.1 字节缓冲流构造方法
9
10 BufferOutputStream:该类实现缓冲输出流。
11 通过设置这样的输出流,应用程序可以向底层输出流写入字节,而不必为写入的每个字节导致底层系统的调用
12
13 BufferedInputStream:创建BufferedInputStream将创建一个内部缓冲区数组。
14 当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次很多字节
15
16 BufferedOutputStream(OutputStream out) 创建字节缓冲输出流对象
17 BufferedInputStream(InputStream in) 创建字节缓冲输入流对象
18
19 为什么构造方法参数为字节流,而不是具体的文件或者路径?
20 字节缓冲流仅仅提供缓冲区,而真正读写数据还得依靠基本的字节流对象进行操作
21 */
22 public class BufferStreamDemo {
23 public static void main(String[] args) throws IOException {
24 /*FileOutputStream fos = new FileOutputStream("stage2\\day9\\lesson1\\bos.txt");
25 BufferedOutputStream bos = new BufferedOutputStream(fos);*/
26
27 /*BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("stage2\\src\\day9\\lesson1\\bos.txt"));
28 bos.write("hello\n".getBytes());
29 bos.write("world\n".getBytes());
30 bos.close();*/
31
32 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("stage2\\src\\day9\\lesson1\\bos.txt"));
33
34 //读取方式1:一次读取一个字节
35 /*int by;
36 while ((by=bis.read()) != -1){
37 System.out.print((char)by);
38 }*/
39
40 //读取方式2:一次读取一个字节数组
41 byte[] bys = new byte[1024];
42 int len;
43 while ((len=bis.read(bys)) != -1){
44 System.out.print(new String(bys, 0, len));
45 }
46
47 bis.close();
48 }
49 }