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


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

    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.耗时

  • 相关阅读:
    在ubuntu20.04下学习shell
    Go 如何实现多态
    Sparse R-CNN细节剖析
    如何在Win系统部署Tomcat服务并实现远程访问内网站点
    083-OCP题库日记
    AI写作生成器-人工智能技术的工具
    使用自定义注解和SpringAOP捕获Service层异常,并处理自定义异常
    关于视频流读取失败的时间问题解决 & 利用修饰符进行限时操作
    国产数据库盘点
    设计模式:适配器模式(论如何把鼠头适配加工成鸭脖)
  • 原文地址:https://blog.csdn.net/qq_45663499/article/details/134097565