码迷,mamicode.com
首页 > 其他好文 > 详细

(六)IO流之过滤流

时间:2018-09-18 00:23:49      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:adc   copy   tput   put   str   dem   buffered   ble   src   

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

标签:adc   copy   tput   put   str   dem   buffered   ble   src   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!