JavaSE-18.1.1【字节缓冲流】

复制代码
 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 }
复制代码

 

posted @   yub4by  阅读(41)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示