• 字节流和字符流的测速


    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();
            }
            
            
        }
        
    
        
    }
  • 相关阅读:
    百度之星 1211 题目一
    acdream 1019 Palindrome 字符串hash
    POJ2480 Longge's problem 欧拉函数
    C#为应用程序注册快捷键
    sql跨数据库查询
    嵌入式轻量级数据库 SQLite和 System.Data.SQLite
    经典问题 C#隐藏和显示窗体
    System.Data.SQLite类Dll下载地址和移植C# SQLite
    CMMI5
    两个ComboBox之间Item的移动
  • 原文地址:https://www.cnblogs.com/lyjblogs/p/7887203.html
Copyright © 2020-2023  润新知