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

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

  • 相关阅读:
    Django-4
    Django-3
    博客中涉及的源码下载
    源码阅读系列:EventBus
    使用DFA做文本编辑器的自动提示
    计算机网络-IP类型判断
    感兴趣的文章搜集
    The Engine Document of JustWeEngine
    [译文]选择使用正确的 Markdown Parser
    Android源码阅读-Filter过滤器
  • 原文地址:https://www.cnblogs.com/lcumin/p/5369008.html
Copyright © 2011-2022 走看看