使用FileChannel实现文件复制
2018-01-10 00:05
295 查看
今天在资深开发者交流,遇到了FileChannel,亲测,在进行文件复制时效果比BufferedInputStream速度要快。
值得一提的是FileChannel在使用前,必须要打开。需要通过InputStream/OutputStream/RandomAccessFile获取,BufferedReader/BufferedWriter获取不到。
使用FileChannel进行文件复制
FileChannel是线程安全的,这就在很多场合我们可以很好的利用这一性能
值得一提的是FileChannel在使用前,必须要打开。需要通过InputStream/OutputStream/RandomAccessFile获取,BufferedReader/BufferedWriter获取不到。
* @see java.io.FileInputStream#getChannel() * @see java.io.FileOutputStream#getChannel() * @see java.io.RandomAccessFile#getChannel()
使用FileChannel进行文件复制
public static void fileChannelCopy(File src,File dst) throws IOException { FileChannel inChannel =new FileInputStream(src).getChannel(); FileChannel outChannel=new FileOutputStream(dst).getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); inChannel.close(); outChannel.close(); }使用普通BufferedInputStream进行文件复制
public static void bufferedCopy(File src,File dst) throws IOException { BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(dst)); int line=0; byte[] buf=new byte[1024*10]; while((line=bis.read(buf))!=-1) { bos.write(buf); } bis.close(); bos.close(); }测试(文件大小1G+)
public static void main(String[] args) throws IOException { File src = new File("E:\\myCode\\kaifa.txt"); File dstFileChannel = new File("E:\\myCode\\kaifa1.txt"); File dstBufferd = new File("E:\\myCode\\kaifa2.txt"); long start = System.currentTimeMillis(); fileChannelCopy(src, dstFileChannel); long end = System.currentTimeMillis(); System.out.println("fileChannelCopy: " + (end - start) + " ms"); start = System.currentTimeMillis(); bufferedCopy(src, dstBufferd); end = System.currentTimeMillis(); System.out.println("bufferedCopy " + (end - start) + " ms"); }我们来比对二者时间消耗
fileChannelCopy: 26370 ms bufferedCopy 38101 ms记在最后:
FileChannel是线程安全的,这就在很多场合我们可以很好的利用这一性能
* <p> File channels are safe for use by multiple concurrent threads. The * {@link Channel#close close} method may be invoked at any time, as specified * by the {@link Channel} interface. Only one operation that involves the * channel's position or can change its file's size may be in progress at any * given time; attempts to initiate a second such operation while the first is * still in progress will block until the first operation completes. Other * operations, in particular those that take an explicit position, may proceed * concurrently; whether they in fact do so is dependent upon the underlying * implementation and is therefore unspecified.
相关文章推荐
- 使用FileChannel(文件通道)来实现文件快速复制
- 学习之使用Java IO 来实现复制文件的操作
- 使用java输入输出流实现文件的复制:
- 使用IO技术,创建一个目录,然后复制一个文件到该目录!实现复制的功能。(在博客园上传的第一份代码)
- java 使用io流对象实现文件复制
- Java实现在复制文件时使用进度条
- 使用shell脚本实现USB设备的加载与文件复制
- 使用java递归实现文件及文件夹的复制
- 使用输入输出流实现文件复制
- 使用splice实现零拷贝复制文件
- IO —— 使用打印流实现文件的复制
- 使用FileChannel复制文件
- Java7中使用try-with-resource语法 实现两种复制文件方法
- 使用JAVA实现比较两个文件夹下的文件新增和修改情况,并复制到新的目录(实现增量更新项目)
- 使用组策略实现文件复制
- 使用mmap即文件映射实现文件的快速复制
- 将指定的文件复制到指定的位置{使用字符流和字节流分别实现}
- 如何使用VBA实现将多个Excel文件中的数据复制到某个Excel文件中
- 使用字节流实现媒体文件的复制
- 使用splice实现零拷贝复制文件