zoukankan      html  css  js  c++  java
  • java第四次作业

    此程序优化读取大的文件(测试文件为14M),写成两个静态方法,方便调用,代码如下:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class Test {
        public static void testFile(File filein, File fileout) throws IOException {
            // 判断文件是否存在
            if (!filein.exists()) {
                System.out.println(filein.getName() + " 不存在 ");
                throw new IOException();
            }
            // 如果文件不存在,创建
            if (!fileout.exists()) {
                fileout.createNewFile();
                throw new IOException();
            }
            FileInputStream in = new FileInputStream(filein);
            FileOutputStream out = new FileOutputStream(fileout);
            int b;
    
            // copy
            while ((b = in.read()) != -1) {
                out.write(b);
            }
            in.close();
            out.close();
        }
    
        
    
        public static void testFileQuick1(File filein, File fileout) throws IOException {
            // 判断文件是否存在
            if (!filein.exists()) {
                System.out.println(filein.getName() + " 不存在 ");
                throw new IOException();
            }
            // 如果文件不存在,创建
            if (!fileout.exists()) {
                fileout.createNewFile();
                throw new IOException();
            }
            FileInputStream in = new FileInputStream(filein);
            FileOutputStream out = new FileOutputStream(fileout);
            byte[] buf = new byte[20 * 1024];
            int bytes = 0;
            // copy
            while ((bytes = in.read(buf, 0, buf.length)) != -1) {
                out.write(buf, 0, bytes);
            }
            in.close();
            out.close();
    
        }
    
        public static void main(String[] args) {
            try {
                long start = System.currentTimeMillis();
                Test.testFile(new File("C:\Users\cumin\Desktop\1.mp3"),
                        new File("C:\Users\cumin\Desktop\6.mp3 "));
                long end = System.currentTimeMillis();
                System.out.println(end - start);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    通过静态方法testFile按字节读取后复制需要:

    Test.testFileQuick1(new File("C:\Users\cumin\Desktop\1.mp3"),
                        new File("C:\Users\cumin\Desktop\6.mp3 "));
                long end = System.currentTimeMillis();
                System.out.println(end - start);

    换成静态方法testFileQuick1后 :

    PS:读取主要通过 read(byte[] b, int off, int len) 提升速度。

      写主要通过 write(byte[] b, int off, int len) 提升速度。

    复制速度大幅提升~~ 而且文件不存在会自动创建。

  • 相关阅读:
    Opencv之像素值的获取
    亚像素级角点检测
    Shi-Tomasi角点检测
    卡尔曼滤波
    Harris角点检测理论
    ROS2GO 与WIN10 双系统安装
    数字集成电路物理设计学习总结——物理设计建库域验证
    跨时钟域信号传输(二)——数据信号篇
    跨时钟域信号传输(一)——控制信号篇
    亚稳态与多时钟切换
  • 原文地址:https://www.cnblogs.com/lcumin/p/5369008.html
Copyright © 2011-2022 走看看