• 原始流,缓冲流性能比较


    一.低级字节流一个一个字节复制

    1.代码

    1. package org.example;
    2. import java.io.*;
    3. public class day13 {
    4. //原视频路径
    5. private static final String file1 = "D:\\temp\\day05\\改名.mp4";
    6. //目的视频路径
    7. private static final String file2 = "D:\\temp\\day05\\不改名.mp4";
    8. public static void main(String[] args) {
    9. copy1();
    10. }
    11. private static void copy1(){
    12. final long startTime = System.currentTimeMillis();
    13. try (InputStream is = new FileInputStream(file1);
    14. OutputStream os = new FileOutputStream(file2);
    15. ) {
    16. int s;
    17. while((s=is.read())!=-1){
    18. os.write(s);
    19. }
    20. }catch (Exception e) {
    21. throw new RuntimeException(e);
    22. }
    23. final long endTime = System.currentTimeMillis();
    24. System.out.println("耗时: "+(endTime-startTime)/1000.0+"s");
    25. }
    26. }

    2.耗时

    二.低级字节流使用字节数组复制

    1.代码

    1. package org.example;
    2. import java.io.FileInputStream;
    3. import java.io.FileOutputStream;
    4. import java.io.InputStream;
    5. import java.io.OutputStream;
    6. public class day14 {
    7. private static final String file1 = "D:\\temp\\day05\\改名.mp4";
    8. //目的视频路径
    9. private static final String file2 = "D:\\temp\\day05\\不改名.mp4";
    10. public static void main(String[] args) {
    11. copy2();
    12. }
    13. private static void copy2(){
    14. final long startTime = System.currentTimeMillis();
    15. try (InputStream is = new FileInputStream(file1);
    16. OutputStream os = new FileOutputStream(file2);
    17. ) {
    18. byte[] buffer = new byte[1024];
    19. int len ;
    20. while((len=is.read(buffer))!=-1){
    21. os.write(buffer,0,len);
    22. }
    23. }catch (Exception e) {
    24. throw new RuntimeException(e);
    25. }
    26. final long endTime = System.currentTimeMillis();
    27. System.out.println("耗时: "+(endTime-startTime)/1000.0+"s");
    28. }
    29. }

    2.耗时

    三.缓冲流一个一个字节复制

    1.代码

    1. package org.example;
    2. import java.io.*;
    3. import java.lang.invoke.VarHandle;
    4. public class day15 {
    5. private static final String file1 = "D:\\temp\\day05\\改名.mp4";
    6. //目的视频路径
    7. private static final String file2 = "D:\\temp\\day05\\不改名.mp4";
    8. public static void main(String[] args) {
    9. copy3();
    10. }
    11. private static void copy3() {
    12. final long startTime = System.currentTimeMillis();
    13. try (InputStream is = new FileInputStream(file1);
    14. BufferedInputStream bis = new BufferedInputStream(is);
    15. OutputStream os = new FileOutputStream(file2);
    16. BufferedOutputStream bos = new BufferedOutputStream(os);
    17. ) {
    18. int len;
    19. while ((len = bis.read()) != -1) {
    20. bos.write(len);
    21. }
    22. } catch (Exception e) {
    23. throw new RuntimeException(e);
    24. }
    25. final long endTime = System.currentTimeMillis();
    26. System.out.println("耗时: " + (endTime - startTime) / 1000.0 + "s");
    27. }
    28. }

    2.耗时

    四.缓冲流使用字节数组复制

    1.代码

    1. package org.example;
    2. import java.io.*;
    3. public class day16 {
    4. private static final String file1 = "D:\\temp\\day05\\改名.mp4";
    5. //目的视频路径
    6. private static final String file2 = "D:\\temp\\day05\\不改名.mp4";
    7. public static void main(String[] args) {
    8. copy4();
    9. }
    10. private static void copy4() {
    11. final long startTime = System.currentTimeMillis();
    12. try (InputStream is = new FileInputStream(file1);
    13. BufferedInputStream bis = new BufferedInputStream(is);
    14. OutputStream os = new FileOutputStream(file2);
    15. BufferedOutputStream bos = new BufferedOutputStream(os);
    16. ) {
    17. byte[] buffer = new byte[1024];
    18. int len;
    19. while ((len = bis.read(buffer)) != -1) {
    20. bos.write(buffer, 0, len);
    21. }
    22. } catch (Exception e) {
    23. throw new RuntimeException(e);
    24. }
    25. final long endTime = System.currentTimeMillis();
    26. System.out.println("耗时: " + (endTime - startTime) / 1000.0 + "s");
    27. }
    28. }

    2.耗时

  • 相关阅读:
    【Laravel系列7.8】广播系统
    【EI检索】2022年电子、通信与控制工程国际会议SECCE 2022
    牛客练习赛115 A Mountain sequence
    更安全、更清晰、更高效——《C++ Core Guidelines解析》
    【1++的C++进阶】之异常
    docker commit 的简单使用
    三月以来5起事故,有限空间作业如何管控?
    【EFK】基于K8S构建EFK+logstash+kafka日志平台
    线阵相机参数介绍---变频参数控制
    vs2019打包其他工具生成的可执行文件
  • 原文地址:https://blog.csdn.net/qq_45663499/article/details/134097565