文件和流过滤器

文件:

1.FilelnputStream

2.FileOutputStream

3.对文件作读写操作

4.实际工程中已经较少使用

5.更常用的是已在内存数据或通信数据上建立的流,如数据库的二进制数据读写或网络端口通信

6.具体的文件读写往往有更专业的类,比如配置文件和日志文件

 byte[] buffers = new byte[10];
        for (int i = 0; i < buffers.length; i++) {
            buffers[i] = (byte) i;
        }
            try {
                FileOutputStream fileOutputStream = new FileOutputStream("a.dat");
                fileOutputStream.write(buffers);
                fileOutputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }在这段代码中我们创建字节输出流FileOutputStream
通过write函数将定义的byte类型的数组进行写入到指定的文件內



流过滤器:
1.以一个介质流对象为基础层层构建过滤器流,最终形成的流对象能在数据的输入输出过程中,逐层使用过滤器流的方法来读写数据
try {
            DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("aaa.txt")));
            int i = 123456;
            out.writeInt(i);
            out.close();
            DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("a.txt")));
            int i1 = in.readInt();
            System.out.println(i1);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

其中在Data中DatalnputStream · DataOutputStream用以读写二进制方式表达的基本数据类型的数据

我们通过将字节流缓冲区放到Data中通过writeInt写入字节,在通过readInt将文件中的字节读出


posted @   冰灵IT  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示