文件和流过滤器

文件

  FilelnputStream

  FileOutputStream

  对文件作读写操作

  实际工程中已经较少使用

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

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

byte[] buf = new byte[10];
for (int i = 0; i < buf.length; i++) {
    buf[i]= (byte) i;
}
try {
    FileOutputStream out = new FileOutputStream("a.dat");
    out.write(buf);
    out.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

 

流过滤器

  以一个介质流对象为基础层层构建过滤器流,最终形成的流对象能在数据的输入输出过程中,逐层使用过滤器流的方式来读写数据

过滤器流:在已经有的那个文件流的基础上去增加一层层的过滤器,在每一层的过滤器可以做一些事情

     DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("a.txt")));
        int i = 123456;
        out.write(i);

        DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("a.txt")));
        int n = in.readInt();
        System.out.println(n);

 

在 FileOutputStream后面去套了一节过滤器,是做DataOutputStream的

BufferedOutputStream做一些缓冲

加上之后就可以写入int类型的一些东西了

DataInputStream可以把数据将10进制的形式打印出来

posted @ 2022-06-23 20:00  漁夫  阅读(46)  评论(0)    收藏  举报