Java 字节缓冲流

阅读 187

2022-02-17

字节缓冲输出流

		//字节缓冲输出流
        FileOutputStream fos = new FileOutputStream("D:\\code\\java\\Stream\\bos.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        //写数据
        bos.write("hello\r\n".getBytes());
        bos.write("world\r\n".getBytes());

        //释放资源
        bos.close();

字节缓冲输入流
		 //字节缓存输入流
        FileInputStream fis = new FileInputStream("D:\\code\\java\\Stream\\bos.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);

        //一次读取一个字节数据
        int by;
        while ((by = bis.read()) != -1) {
            System.out.print((char)by);
        }

        //释放资源
        bis.close();

一次读取一个字节数组的方式

		//一次读取一个字节数组
        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1) {
            System.out.print(new String(bytes,0,len));
        }

精彩评论(0)

0 0 举报