字节流和字符流的测速

package Io;

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;

/**
 *         比较各个流复制文件的速度(文件大小127MB)
 * @author LYJ        
 *                     IO流  输入输出                                                               文件大小(MB)          花费时间    (毫秒)        
 *    1. FileInputStream FileOutputStream                127                    2130
 *    2. BufferedInputStream BufferedOutputStream         127                    334
 *    3. FileReader  FileWriter                         127                       4275
 *    4. BufferedReader BufferedWriter                 127                    4478
 */
public class TestSpeed {
    private File f1 = null;
    private File f2 = null;
    private File f3 = null;
    private FileInputStream fis = null;
    private FileOutputStream fos = null;
    private int len = 0;
    private byte [] b = new byte[1024];
    private long start = 0;
    private long end = 0;
    private char [] ch = new char[1024];
    
    /**********************************************************************************************
     *     字节流
     *    FileInputStream   FileOutPutStram
     */
//    @Test
//    public void inputstream() {
//        start=System.currentTimeMillis();
//        try {
//            f1 = new File("F://a//a.exe");
//            f2 = new File("E://1");
//            f3 = new File(f2,"a.exe");
//            if(!f2.exists()) {
//                f2.mkdirs();
//            }
//            fis = new FileInputStream(f1);
//            fos = new FileOutputStream(f3);
//            while((len = fis.read(b))!=-1) {
//                fos.write(b,0,len);
//            }
//            end = System.currentTimeMillis();
//            System.out.println("拷贝成功~"+"花费时间(毫秒):"+(end-start));
//            
//            
//            
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        
//        
//    }
    /***************************************************************************************/
    /**
     * 
     * 缓冲字符流 BufferedInputStream BufferedOutputStream
     * 
//     */
//    @Test
//    public void BufferedByte() {
//        start=System.currentTimeMillis();
//        try {
//            f1 = new File("F://a//a.exe");
//            f2 = new File("E://1");
//            f3 = new File(f2,"a.exe");
//            if(!f2.exists()) {
//                f2.mkdirs();
//            }
//            fis = new FileInputStream(f1);
//            BufferedInputStream bis = new BufferedInputStream(fis);
//            fos = new FileOutputStream(f3);
//            BufferedOutputStream bos = new BufferedOutputStream(fos);
//            while((len = bis.read(b))!=-1) {
//                bos.write(b,0,len);
//            }
//            end = System.currentTimeMillis();
//            System.out.println("拷贝成功~"+"花费时间(毫秒):"+(end-start));
//            
//            
//            
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        
//    }
//    
//    /*************************************************************************************************/
//    /**字符流
//     * FileReader   FileWriter
//     */
//    @Test
//    public void FileFW() {
//        start=System.currentTimeMillis();
//        try {
//            f1 = new File("F://a//a.exe");
//            f2 = new File("E://1");
//            f3 = new File(f2,"a.exe");
//            if(!f2.exists()) {
//                f2.mkdirs();
//            }
//            FileReader fr = new FileReader(f1);
//            FileWriter fw = new FileWriter(f3);
//            while((len = fr.read(ch))!=-1) {
//                fw.write(ch,0,len);
//            }
//            end = System.currentTimeMillis();
//            System.out.println("拷贝成功~"+"花费时间(毫秒):"+(end-start));
//            
//            
//            
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        
//        
//    }
    
    /*********************************************************************************************/
    @Test
    public void BufferedFW() {
        start=System.currentTimeMillis();
        try {
            f1 = new File("F://a//a.exe");
            f2 = new File("E://1");
            f3 = new File(f2,"a.exe");
            if(!f2.exists()) {
                f2.mkdirs();
            }
            FileReader fr = new FileReader(f1);
            BufferedReader br = new BufferedReader(fr);
            FileWriter fw = new FileWriter(f3);
            BufferedWriter bw = new BufferedWriter(fw);
            while((len = br.read(ch))!=-1) {
                bw.write(ch,0,len);
            }
            end = System.currentTimeMillis();
            System.out.println("拷贝成功~"+"花费时间(毫秒):"+(end-start));
            
            
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
    }
    

    
}

 

posted @ 2017-11-23 21:57  默默向风呆  阅读(322)  评论(0编辑  收藏  举报