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.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();//花费时间:3289
    }

    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(car,0,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));
    }
}

posted @ 2021-07-17 16:36  贪玩的阿柒  阅读(159)  评论(0编辑  收藏  举报