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

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

  • 相关阅读:
    spool使用
    webservice项目部署部署到weblogic报错之解决方案
    cxf , struts+spring中web.xml过滤url问题解决方案
    eclipse配置SVN
    Oracle中的dual表
    tomcat在eclipse启动成功却打不开tomcat主页
    java作用域public ,private ,protected
    final static
    sql server 通明加密
    检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。
  • 原文地址:https://www.cnblogs.com/lcumin/p/5369008.html
Copyright © 2011-2022 走看看