zoukankan      html  css  js  c++  java
  • 封装了集中常用的文件读的方法

    package com.opslab.util.algorithmImpl;

    import java.io.*;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;

    /**
    * 封装了集中常用的文件读的方法
    */
    public class FileReadImpl {

    /**
    * 利用FileChannel直接实现文件的对拷,对于大文件速度特别明显
    *
    * @param source
    * @param target
    */
    public static void copyFileWithChannel(File source, File target) {
    try (
    FileInputStream inStream = new FileInputStream(source);
    FileOutputStream outStream = new FileOutputStream(target);
    FileChannel in = inStream.getChannel();
    FileChannel out = outStream.getChannel();
    ) {
    in.transferTo(0, in.size(), out);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**
    * 使用FileChannel+Buffer实现文件的读取拷贝是一种极佳的方案
    *
    * @param source
    * @param target
    */
    public static void copyFileWithBuffer(File source, File target) {
    try (
    FileInputStream inStream = new FileInputStream(source);
    FileOutputStream outStream = new FileOutputStream(target);
    FileChannel in = inStream.getChannel();
    FileChannel out = outStream.getChannel()
    ) {
    ByteBuffer buffer = ByteBuffer.allocate(4096);
    while (in.read(buffer) != -1) {
    buffer.flip();
    out.write(buffer);
    buffer.clear();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**
    * 利用Buffer实现文件的读取拷贝
    *
    * @param source
    * @param target
    */
    public static void customBufferBufferedStreamCopy(File source, File target) {
    try (
    InputStream fis = new BufferedInputStream(new FileInputStream(source));
    OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))
    ) {
    byte[] buf = new byte[4096];
    int i;
    while ((i = fis.read(buf)) != -1) {
    fos.write(buf, 0, i);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**
    * 利用Buffer实现文件的读取拷贝
    *
    * @param source
    * @param target
    */
    public static void customBufferStreamCopy(File source, File target) {
    try (
    InputStream fis = new FileInputStream(source);
    OutputStream fos = new FileOutputStream(target);
    ) {
    byte[] buf = new byte[4096];
    int i;
    while ((i = fis.read(buf)) != -1) {
    fos.write(buf, 0, i);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    }

  • 相关阅读:
    Reloading Java Classes 301: Classloaders in Web Development — Tomcat, GlassFish, OSGi, Tapestry 5 and so on Translation
    Chapter 6 -- Caches
    SVN OPS发布总结
    Chapter 5 -- ImmutableCollections
    差点掉坑,MySQL一致性读原来是有条件的
    PHP实现的一个时间帮助类
    H5拍照、选择图片上传组件核心
    Webpack + Vue 多页面项目升级 Webpack 4 以及打包优化
    javascript-函数表达式
    javascript遍历方法总结
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10254640.html
Copyright © 2011-2022 走看看