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

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

  • 相关阅读:
    ASP.NET图片上传(配合jquery.from.js 插件)
    判断上传文件类型,上传图片
    父子一对多iframe,子iframe改子iframe元素
    Jquery 清空input file的值
    通过createObjectURL实现图片预览
    URL.createObjectURL() 与 URL.revokeObjectURL()
    python try finally和with语句
    python mixin 模式特点
    Python中的Sentinel(哨兵)值
    《JavaScript ES6 函数式编程入门经典》笔记1
  • 原文地址:https://www.cnblogs.com/lcumin/p/5369008.html
Copyright © 2011-2022 走看看