首页 > 其他 > 详细

(六)IO流之过滤流

时间:2018-09-17 23:24:35      阅读:229      评论:0      收藏:0      [点我收藏+]

过滤字节流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流之过滤流

原文:https://www.cnblogs.com/tanlei-sxs/p/9665568.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!