采用Java语言如何实现高速文件复制?
今天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语言如何实现高速文件复制?的更多相关文章
- java 20 - 8 字节流的文件复制以及汉字在计算机中的存储方式
复制文本文件:把当前目录下的FileIntputStream.java文件里面的内容复制到当前目录的b.txt文件中 分析: 数据源: FileIntputStream.java -- 读取数据 -- ...
- JAVA(IO流)文件复制
package com.am; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOEx ...
- java使用字节流和字符流实现文件复制
大家在Java开发中都会遇到文件复制的文件,众所周知,需要通过文件输入输出流实现. 那究竟该怎么做那,话不多说,直接上代码: 一,使用字节流复制文件 public class FileByteCopy ...
- 2020重新出发,JAVA语言,什么是JAVA?
@ 目录 什么是 java? JAVA三大体系 Java SE Java EE JavaME java的主要特性和优势 1. 面向对象 2. 平台无关性 3. 可移植性 4. 简单性 5. 解释执行 ...
- 微信公众平台应用开发:方法、技巧与案例--柳峰,Java语言版本
他本人的博客:http://blog.csdn.net/lyq8479 作者简介: 刘运强,网名“柳峰”,资深微信公众平台应用开发工程师,国内微信公众平台应用开发的先驱之一,项目经验丰富.他还是一位资 ...
- 四则运算程序扩展:将程序改为java语言,并允许用户输入,对输入结果进行验证
题目 每个同学选一个方向,把程序扩展一下:1.让程序能接受用户输入答案,并判定对错.最后给出总共对/错 的数量.2.把程序变成一个网页程序,用户通过设定参数,就可以得到各种题目.3.把程序变成一个Wi ...
- IT兄弟连 Java语法教程 Java语言的跨平台特性
什么是平台 Java是可以跨平台的编程语言,那么首先我们需要知道什么是平台,通常我们把CPU与操作系统的整体称为平台. CPU大家都知道,是计算机的大脑,它既负责思维运算,又负责计算机中各种零部件的命 ...
- 基于Java语言的IO操作(文件复制)
public static void main(String[] args) { //获取复制开始前系统时间毫秒值 long start=System.currentTimeMillis(); //文 ...
- java中的IO流之文件复制
O(∩_∩)O哈哈~ 1.综述 一门成熟的语言肯定具备的几个模块:IO,通信,线程,UI...... Java作为一门成熟的程序语言,其IO流是比较复杂的.上个图大家感受下: 简单分析一下,IO分为两 ...
随机推荐
- hdu4059 The Boss on Mars
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 佳文分享:CAP定理
1976年6月4号,周5,在远离音乐会大厅的一个楼上的房间内,在位于Manchester的Lesser Free Trade Hall ,Sex Pistols 乐队(注:Sex Pistols的经理 ...
- 从零开始学C++之动态创建对象
回顾前面的文章,实现了一个简单工厂模式来创建不同类对象,但由于c++没有类似new "Circle"之类的语法,导致CreateShape 函 数中需要不断地ifelse地去判断, ...
- NET实现的DDD、CQRS与微服务架构
WeText项目:一个基于.NET实现的DDD.CQRS与微服务架构的演示案例 最近出于工作需要,了解了一下微服务架构(Microservice Architecture,MSA).我经过两周业余时间 ...
- linux LVS DR模式配置
拓扑图: 测试环境:CentOS 6.5 X86 64位 配置步骤: 1. 安装测试环境 [root@UCS-1 ~]# yum -y install httpd [root@UCS-1 ~]# c ...
- [LeetCode]Pascal's Triangle II
题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...
- red hat Linux 使用CentOS yum源更新
red hat linux是商业版软件,没有经过注册是无法使用红帽 yum源更新软件的,使用CentOS源更新操作如下: 1.删除red hat linux 原有的yum 源 rpm -aq | gr ...
- 23、Cocos2dx 3.0游戏开发找小三之粒子系统:你那里下雪了吗?
重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/30485919 春雨惊春清谷天,夏满芒夏暑相连, 秋处 ...
- 重构后的ConditionHelper
两三个月前曾写过<重构ConditionHelper>的随笔,但不知是因为写得不够好还是没有什么新意,我发表至博客园首页时被屏蔽了,本着好的知识应该分享给更多人,加之新项目已交付用户使用所 ...
- c#程序内存分配
c#程序内存分配 进程可使用内存数就是操作系统给进程分配的最大地址,一般的32位操作系统提供给用户地址最大都是3g(操作系统自己保留1g),windows由于商业目的,对于个人用户只提供了2g地址,要 ...