IO流之过滤流

Posted tanlei-sxs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IO流之过滤流相关的知识,希望对你有一定的参考价值。

过滤字节流FilterInputStream和FilterOutputStream

BufferedInputStream和BufferedOutputStream

package com.tanlei.InputOutputStream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferedInputOutputDemo {
    public static void main(String[] args) {
        try {
            FileUtil1.copyFile(new File("C:\zhangsan\tanlei.txt"), new File("D:\zhangsan.txt"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } 
}
class FileUtil1{
    public  static  void  copyFile(File src,File dst) throws IOException {
        FileInputStream fis=new FileInputStream(src);
        FileOutputStream fos=new FileOutputStream(dst);
        BufferedInputStream bis=new BufferedInputStream(fis);
        BufferedOutputStream bos=new BufferedOutputStream(fos);
        
        long time=System.currentTimeMillis();
        int data=0;//用来保存实际读到的字节数
        while ((data=bis.read())!=-1) {
            bos.write(data);
        }
        bis.close();
        bos.close();
        long time2=System.currentTimeMillis();
        System.out.println("共花费+"+(time2-time));
    }
}

DataInputStream和DataOutputStream

package com.tanlei.InputOutputStream;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataInputOutputDemo {
   public static void main(String[] args) throws IOException {
     String name="tanlei";
     int age=20;
     boolean flag=true;
     char sex=‘男‘;
     double money=110.56;
     
     DataOutputStream dos=new DataOutputStream(new FileOutputStream("D:\aa.txt"));
     dos.writeUTF(name);
     dos.writeInt(age);
     dos.writeDouble(money);
     dos.writeChar(sex);
     dos.writeBoolean(flag);
     dos.close();
     
     DataInputStream dis=new DataInputStream(new FileInputStream("D:\\aa.txt"));
     System.out.println(dis.readUTF());
     System.out.println(dis.readInt());
     System.out.println(dis.readDouble());
     System.out.println(dis.readChar());
     System.out.println(dis.readBoolean());
     dis.close();
   }
}

总结:

提供带缓冲的读写

以上是关于IO流之过滤流的主要内容,如果未能解决你的问题,请参考以下文章

IO流之字符输入流,字符输出流

IO流之再战猜拳小游戏

IO流之IO流综述

IO流之缓冲流

io流之转换流InputStreamReaderOutputStreamWriter

IO流之字节,字符