• 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) 提升速度。

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

  • 相关阅读:
    从苹果系统InstallESD.dmg里提取IOS
    Swift编程语言中文版教程---《The Swift Programming Language》
    VMware workstation 10.0的永久key
    VMware Workstation与Hyper-V不兼容。请先从系统中移除Hyper-V角色,然后再运行VMware Workstation。
    Unity3D游戏制作入门教程
    MyEclipse中文网
    机器学习中的两个概率模型
    机器学习中的两个概率模型
    EM算法概念
    EM算法概念
  • 原文地址:https://www.cnblogs.com/lcumin/p/5369008.html
Copyright © 2020-2023  润新知