JAVA面试必备基础知识点

第十二章、Java中的文件拷贝方式

2018-10-29  本文已影响6人  小母牛不生产奶

使用输入输出流进行读写时,实际上是进行了多次上下文切换,比如应用读取数据时,先在内核态讲数据从磁盘读取到内核缓存,再切换到用户态将数据从内核缓存读取到用户缓存。

输入输出流方式

NIO transferTo的实现方式,在Linux和Unix上,会使用零拷贝技术,数据传输并不需要用户态参与,省去了上下午切换的开销和不必要的内存拷贝,进而可提高应用拷贝性能。

nio方式

buffer是NIO操作数据的基本工具,java为每种数据类型都提供了相应的buffer实现(布尔除外)

Buffer 有几个基本属性:

capcity:它反映这个buffer到底多大,也就是数据长度

postion:要操作的数据起始位置

limit:相当于操作的限额,在读取或者写入时,limit 的意义很明显是不一样的。比如,读取操作时,很可能将 limit 设置到所容纳数据的上限;而在写入时,则会设置容量或容量以下的可写限度。

mark,记录上一次postion的位置,默认是0

buffer类型

总结

如何提高类似拷贝等IO操作的性能原则:

1、合理使用缓存等机制,减少IO次数;

2、使用transferTo等机制,减少上下文切换和额外IO操作;

3、尽量减少不必要的转换过程,比如编解码;对象序列化和反序列化,比如操作文本文件或者网络通信,如果不是过程中需要使用文本信息,可以考虑不要将二进制信息转换成字符串,直接传输二进制信息

/**

* This file created at 2018年10月29日.

*/

package io;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.nio.channels.FileChannel;

import java.nio.file.Files;

import org.apache.commons.io.FileUtils;

/**

* <code>{@link MAIN}</code>

*

* TODO : document me

*

* @author yabushan

*/

public class MAIN {

public static void main(String[] args) throws IOException {

copyFileUsingFileStreams(new File("D:/表结构.xlsx"), new File("D:/表结构2.xlsx"));

copyFileUsingFileChannels(new File("D:/表结构.xlsx"), new File("D:/表结构3.xlsx"));

copyFileUsingApacheCommonsIO(new File("D:/表结构.xlsx"), new File("D:/表结构4.xlsx"));

copyFileByChannel(new File("D:/表结构.xlsx"), new File("D:/表结构5.xlsx"));

}

/**

* 1. 使用FileStreams复制

* 这是最经典的方式将一个文件的内容复制到另一个文件中。

*  使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B

* @param source

* @param dest

* @throws IOException

*/

private static void copyFileUsingFileStreams(File source, File dest)

        throws IOException {   

    InputStream input = null;   

    OutputStream output = null;   

    try {

          input = new FileInputStream(source);

          output = new FileOutputStream(dest);       

          byte[] buf = new byte[1024];       

          int bytesRead;       

          while ((bytesRead = input.read(buf)) > 0) {

              output.write(buf, 0, bytesRead);

          }

    } finally {

        input.close();

        output.close();

    }

}

/**

*

* 2. 使用FileChannel复制

* Java NIO包括transferFrom方法,根据文档应该比文件流复制的速度更快

* * @param source

* @param dest

* @throws IOException

*/

private static void copyFileUsingFileChannels(File source, File dest)

throws IOException {   

        FileChannel inputChannel = null;   

        FileChannel outputChannel = null;   

    try {

        inputChannel = new FileInputStream(source).getChannel();

        outputChannel = new FileOutputStream(dest).getChannel();

        outputChannel.transferFrom(inputChannel, 0, inputChannel.size());

    } finally {

        inputChannel.close();

        outputChannel.close();

    }

}

/**

* 3. 使用Commons IO复制

* Apache Commons IO提供拷贝文件方法在其FileUtils类,

*可用于复制一个文件到另一个地方。

*它非常方便使用Apache Commons FileUtils类时,您已经使用您的项目。

*基本上,这个类使用Java NIO FileChannel内部

* @param source

* @param dest

* @throws IOException

*/

private static void copyFileUsingApacheCommonsIO(File source, File dest)

        throws IOException {

    FileUtils.copyFile(source, dest);

}

/**

* 4. 使用Java7的Files类复制

    * 如果你有一些经验在Java 7中你可能会知道,

    * 可以使用复制方法的Files类文件,从一个文件复制到另一个文件

* @param source

* @param dest

* @throws IOException

*/

private static void copyFileUsingJava7Files(File source, File dest)

        throws IOException {   

        Files.copy(source.toPath(), dest.toPath());

}

/**

* 利用java.nio类库提供的transferTo或transferFrom方法实现

* @param source

* @param target

* @throws IOException

*/

public static void copyFileByChannel(File source, File target) throws IOException {

  try (FileChannel sc = new FileInputStream(source).getChannel();

    FileChannel tc = new FileOutputStream(target).getChannel();) {

    long count = sc.size();

    while (count > 0) {

      long transferred = sc.transferTo(sc.position(), count, tc);

      count -= transferred;

    }

  }

}

}

上一篇下一篇

猜你喜欢

热点阅读