0
点赞
收藏
分享

微信扫一扫

Java 字节缓冲流

婉殇成长笔记 2022-02-17 阅读 187

字节缓冲输出流

		//字节缓冲输出流
        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 条评论