1. 程式人生 > 其它 >Java位元組流和字元流的複製案例及測速

Java位元組流和字元流的複製案例及測速

Java位元組流和字元流的複製案例及測速

案例

package com.cnblogs;

import java.io.*;
import java.util.Scanner;

public class TestCopyFile {
    public static void main(String[] args) {
        System.out.println("請輸入您要複製的原始檔路徑:");
        String f = new Scanner(System.in).nextLine();
        System.out.println("請輸入您目標檔案的路徑:");
        String t = new Scanner(System.in).nextLine();

        InputStream in1 = null;
        OutputStream out1 = null;

        try {
            in1 =new BufferedInputStream(new FileInputStream(f)) ;
            out1 =new BufferedOutputStream(new FileOutputStream(t)) ;
            int result;
            while((result = in1.read()) != -1){
                out1.write(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


/*
字元流複製的圖片沒法開啟,位元組流可以。
 */
//
////        Reader in = null;
////        Writer out = null;
////
////        try {
////            in = new BufferedReader(new FileReader(f));
////            out = new BufferedWriter(new FileWriter(t));
////            int result = 0;
////            while ((result = in.read()) != -1){
////                out.write(result);
////            }
////            System.out.println("檔案複製成功!");
////        } catch (Exception e) {
////            System.out.println("檔案複製失敗!");
////            e.printStackTrace();
////            /*
////            一定會執行的程式碼需要放在finally{}中,比如關流
////            關流操作是有順序的,如果有多個流,先建立的後關閉
////            多條關流語句需要各自try-catch
////             */
////        } finally {
////            try {
////                out.flush();
////                out.close();
////            } catch (IOException e) {
////                e.printStackTrace();
////            }
////            try {
////                in.close();
////            } catch (IOException e) {
////                e.printStackTrace();
////            }
////        }
    }
}

總結:

​ 字元流只能複製文字檔案,複製的圖片,視訊儘管可以複製,但會造成檔案損壞,

而位元組流可以實現完美的複製,不會損壞檔案,位元組流更接近底層。

測速

package com.cnblogs.api;

import java.io.*;
/*
測試速度,用136M的視訊檔案來做複製貼上實驗
 */
public class TestSpeed {
    public static void main(String[] args) {
//        method1();//花費時間:429937
//        method2();//花費時間:4308
//        method3();//花費時間:12858
        method4();//花費時間:8245
//        method5();//花費時間:1218
    }

    private static void method5() {
        //測試BufferedInputStream和BufferedOutputStream的速度 加上“車”
        long start = System.currentTimeMillis();
        InputStream in1 = null;
        OutputStream out1 = null;
        try {
            in1 =new BufferedInputStream(new FileInputStream("E:\\1.wmv")) ;
            out1 =new BufferedOutputStream(new FileOutputStream("E:\\study\\1.wmv")) ;
            int result;
            byte[] car = new byte[1024*1024*1024];
            while((result = in1.read(car)) != -1){
                out1.write(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("花費時間:" + (end - start));
    }

    private static void method4() {
        //測試BufferedReader和BufferedWriter的速度
        long start = System.currentTimeMillis();
        Reader in1 = null;
        Writer out1 = null;
        try {
            in1 =new BufferedReader(new FileReader("E:\\1.wmv"));
            out1 =new BufferedWriter(new FileWriter("E:\\study\\1.wmv"));
            int result;
            while((result = in1.read()) != -1){
                out1.write(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("花費時間:" + (end - start));
    }

    private static void method3() {
        //測試FileReader和FileWriter的速度
        long start = System.currentTimeMillis();
        Reader in1 = null;
        Writer out1 = null;
        try {
            in1 = new FileReader("E:\\1.wmv") ;
            out1 = new FileWriter("E:\\study\\1.wmv") ;
            int result;
            while((result = in1.read()) != -1){
                out1.write(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("花費時間:" + (end - start));
    }


    private static void method2() {
        //測試BufferedInputStream和BufferedOutputStream的速度
        long start = System.currentTimeMillis();
        InputStream in1 = null;
        OutputStream out1 = null;
        try {
            in1 =new BufferedInputStream(new FileInputStream("E:\\1.wmv")) ;
            out1 =new BufferedOutputStream(new FileOutputStream("E:\\study\\1.wmv")) ;
            int result;
            while((result = in1.read()) != -1){
                out1.write(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("花費時間:" + (end - start));
    }

    private static void method1() {
        //測試FileInputStream和FileOutputStream的速度
        long start = System.currentTimeMillis();
        InputStream in1 = null;
        OutputStream out1 = null;
        try {
            in1 = new FileInputStream("E:\\1.wmv");
            out1 = new FileOutputStream("E:\\study\\1.wmv");
            int result;
            while((result = in1.read()) != -1){
                out1.write(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("花費時間:" + (end - start));
    }
}