首页 > 其他 > 详细

处理流_缓冲流

时间:2014-12-11 01:29:02      阅读:220      评论:0      收藏:0      [点我收藏+]

1 和节点流相比,可以提升文件操作的效率

2 flush操作将最后缓存中剩余的字符串输出

package lianxi1;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.Test;

public class TestBuffered {
@Test
 public void testCopy(){
    long start = System.currentTimeMillis();
    String src = "d:/io/plan.png";
    String des = "prison.png";
    copyFile(src,des);
    long end = System.currentTimeMillis();
    System.out.println(end-start);
}
  public void copyFile(String src,String des){
    FileInputStream fis = null;
    FileOutputStream fos = null;
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    File file1 = new File(src);
    File file2 = new File(des);
    try{
        fis = new FileInputStream(file1);
        fos = new FileOutputStream(file2);
        bis = new BufferedInputStream(fis);
        bos = new BufferedOutputStream(fos);
        int len;
        byte[] b = new byte[100];
        while((len=bis.read(b))!=-1){
            bos.write(b, 0, len);
            bos.flush();
        }
    }catch(Exception ex){ex.printStackTrace();}
    finally{
        if(bis!=null){
            try {
                bis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(bos!=null){
            try {
                bos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
@Test
   public void test2(){
    FileReader fr = null;
    FileWriter fw = null;
    BufferedReader br = null;
    BufferedWriter bw = null;
    File file1 = new File("d:/io/hellotext.txt");
    File file2 = new File("Tangshan");
    try{
        fr = new FileReader(file1);
        fw = new FileWriter(file2);
        br = new BufferedReader(fr);
        bw = new BufferedWriter(fw);
//        int len;
//        char[] c = new char[100];
//        while((len=br.read(c))!=-1){
//            bw.write(c, 0, len);
//            bw.flush();
//        }
        //或用readline方法
        String str;
        while((str=br.readLine())!=null){
            bw.write(str);
            bw.newLine();
        }
    }catch(Exception ex){ex.printStackTrace();}
    finally{
        if(br!=null){
            try {
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(bw!=null){
            try {
                bw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
}

处理流_缓冲流

原文:http://www.cnblogs.com/yjtm53/p/4156644.html

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