今天review代码又看到某个“大神”使用古老的方法来实现文件的复制,今天归结一下使用Java语言如何实现快速文件复制:

代码1——使用文件通道的方式:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;public class Test {public static void main(String[] args){long start = System.currentTimeMillis();FileInputStream fileInputStream = null;FileOutputStream fileOutputStream = null;FileChannel inFileChannel = null;FileChannel outFileChannel = null;try {fileInputStream = new FileInputStream(new File("C:\\from\\不是闹着玩的.flv"));fileOutputStream = new FileOutputStream(new File("C:\\to\\不是闹着玩的.flv"));inFileChannel = fileInputStream.getChannel();outFileChannel = fileOutputStream.getChannel();inFileChannel.transferTo(0, inFileChannel.size(), outFileChannel);//连接两个通道,从in通道读取数据写入out通道。} catch (IOException e) {e.printStackTrace();} finally {try {if(fileInputStream != null){fileInputStream.close();}if(inFileChannel != null){inFileChannel.close();}if(fileOutputStream != null){fileOutputStream.close();}if(outFileChannel != null){outFileChannel.close();}} catch (IOException e) {e.printStackTrace();}}long end = System.currentTimeMillis();System.out.println("视频文件从“from”文件夹复制到“to”文件需要" + (end - start) + "毫秒。");}
}

代码运行结果为:

代码2——使用缓冲输入输出流

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;public class Test {public static void main(String[] args){long start = System.currentTimeMillis();InputStream fileInputStream = null;OutputStream fileOutputStream = null;try {fileInputStream = new BufferedInputStream( new FileInputStream(new File("C:\\from\\不是闹着玩的.flv")));fileOutputStream = new BufferedOutputStream(new FileOutputStream(new File("C:\\to\\不是闹着玩的.flv")));byte[] bufferArray = new byte[1024*1024];   int length;   while ((length = fileInputStream.read(bufferArray)) != -1) {   fileOutputStream.write(bufferArray, 0, length);   }  } catch (IOException e) {e.printStackTrace();} finally {try {if(fileInputStream != null){fileInputStream.close();}if(fileOutputStream != null){fileOutputStream.close();}} catch (IOException e) {e.printStackTrace();}}long end = System.currentTimeMillis();System.out.println("视频文件从“from”文件夹复制到“to”文件需要" + (end - start) + "毫秒。");}
}

代码运行结果为:
  

代码3——使用文件输入输出流的方式:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class Test {public static void main(String[] args){long start = System.currentTimeMillis();FileInputStream fileInputStream = null;FileOutputStream fileOutputStream = null;try{fileInputStream = new FileInputStream(new File("C:\\from\\不是闹着玩的.flv")); //读入原文件 fileOutputStream = new FileOutputStream("C:\\to\\不是闹着玩的.flv");byte[] bufferArray = new byte[1024*1024]; int length; while ((length = fileInputStream.read(bufferArray)) != -1) { fileOutputStream.write(bufferArray, 0, length); }} catch (IOException e) {e.printStackTrace();  } finally {try {if(fileInputStream != null){fileInputStream.close();}if(fileOutputStream != null){fileOutputStream.close();}} catch (IOException e) {e.printStackTrace();}}long end = System.currentTimeMillis();System.out.println("视频文件从“from”文件夹复制到“to”文件需要" + (end - start) + "毫秒。");}
}

代码运行结果为:

代码1、代码2和代码3复制的是相同的文件,通过比较不难得出结论:在文件复制时尽量使用文件输入输出流可能是比较好的办法。

使用Java语言如何实现快速文件复制?相关推荐

  1. 怎样用java写一个简单的文件复制程序

    怎样用java写一个简单的文件复制程序 代码来源:https://jingyan.baidu.com/article/c35dbcb0d6f1398916fcbc07.html package Num ...

  2. Java学习总结:44(文件复制案例)

    案例:文件复制(针对InputStream和OutputStream的操作应用) 流程图(比较复杂我就不敲了,直接拍出来) 例:实现文件复制操作 package Project.Study.FileC ...

  3. java之通过FileChannel实现文件复制

    1.FileChanel介绍 Java NIO FileChannel是连接文件的通道,从文件中读取数据和将数据写入文件.Java NIO FileChannel类是NIO用于替代使用标准Java I ...

  4. IntelliJ IDEA 中的Java Web项目的资源文件复制新增如何更新到部署包中?

    文章目录 resources目录下的文件复制新增 webapp目录的文件复制新增 IntelliJ IDEA 中的Java Web项目的资源文件如果复制新增,正常的重新部署.重启服务器.update ...

  5. ExtremeCopy 2.3.2 简体中文注册版 | 快速文件复制工具

    ExtremeCopy Pro 2.3.2 简体中文注册版是一款Windows文件复制移动增强软件,它会根据硬件和系统资源自动优化文件复制和移动速度,可以在拷贝过程中暂停,支持通配符,可以直接使用Ex ...

  6. Java IO 、 File 、 文件复制

    目录 1.1转换流 1.1.1概述 1.1.2 InputStreamReader 1.2打印流 1.2.1概述 1.2.2 使用 1.3 对象流 / 序列化 1.3.1 概述 1.3.2序列化 1. ...

  7. JAVA语言 - Android拷贝assets文件(资源文件)

    前言 Android应用开发,资源文件一般是放在assets路径下,这些资源文件可以是: 图片文件(jpg png bmp) 音频文件(wav mp3) 文本文件(txt) 模型文件 应用启动之后,需 ...

  8. JAVA语言实现操作Excel文件

    导入excel相关 jar 包 java import jxl.*;import jxl.write.*; 读取Excel文件 javaimport java.io.File; public void ...

  9. java语言怎样判断文件夹_JAVA语言之如何判断文件,判断文件夹是否存在的代码...

    本文主要向大家介绍了JAVA语言之如何判断文件,判断文件夹是否存在的代码,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助. 一.判断文件是否存在,不存在则创建File file = ne ...

  10. Jeewx捷微 , 免费微信公众账号管家系统发布,采用JAVA语言

    JeeWx, 微信管家平台,简称"捷微". 捷微是一款免费开源的JAVA微信公众账号开发平台. 当前最新版本: 2.3(发布日期:20160323)   平台介绍: 一.捷微Jee ...

最新文章

  1. python image stiching_Python自然语言处理,词云图生成
  2. ExtJS 4.2菜单
  3. (二十九)、Java字符串中去除空格
  4. Golang 常见设计模式之装饰模式
  5. 【详解】CSS阴影用法——Web前端系列学习笔记
  6. 【终极方法】This method must return a result of type boolean
  7. 语言求余和乘除优先级_愉快地学Java语言:第二章基本程序设计 第2讲
  8. jbpm小项目测试_尝试使用jBPM Console NG(测试版)
  9. chackbox的值 php获取_最详细最全的PHP面试题(附答案)
  10. 阿里箫疯:AI对抗攻击—原理及预防 (精彩直播回放)
  11. win7旗舰版+caffe+vs2013+matlab2014b(无GPU版)
  12. python网站访问日志分析_python分析apache网站日志web日志的代码
  13. 安装mujoco报错:distutils.errors.DistutilsExecError: command ‘gcc‘ failed with exit status 1
  14. 转(static final 和final的区别)
  15. vim编辑文件时显示行号
  16. 原 SpringFramework核心技术五:Spring AOP API
  17. 购物直播APP系统+短视频系统开发方案
  18. 计算机扫描的配置文件在哪里找,打印机扫描文件到电脑哪里找
  19. python 发送邮件正文字体设置_smtplib文字邮件的发送
  20. 克隆vm虚拟机详细步骤

热门文章

  1. C#语句之while语句
  2. 虚函数如何实现多态 ?
  3. 删除.svn 隐藏文件
  4. Docker 的部署方式
  5. InteliiJ IDEA的安装配置与简单使用
  6. 一致性Hash与负载均衡
  7. 基于Windows NBL配置WebInterface
  8. 农民伯伯 谈 接口 [interface]
  9. 1405 奶牛的旅行
  10. C#编程总结(十一)数字证书