今天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 20 - 8 字节流的文件复制以及汉字在计算机中的存储方式

    复制文本文件:把当前目录下的FileIntputStream.java文件里面的内容复制到当前目录的b.txt文件中 分析: 数据源: FileIntputStream.java -- 读取数据 -- ...

  2. JAVA(IO流)文件复制

    package com.am; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOEx ...

  3. java使用字节流和字符流实现文件复制

    大家在Java开发中都会遇到文件复制的文件,众所周知,需要通过文件输入输出流实现. 那究竟该怎么做那,话不多说,直接上代码: 一,使用字节流复制文件 public class FileByteCopy ...

  4. 2020重新出发,JAVA语言,什么是JAVA?

    @ 目录 什么是 java? JAVA三大体系 Java SE Java EE JavaME java的主要特性和优势 1. 面向对象 2. 平台无关性 3. 可移植性 4. 简单性 5. 解释执行 ...

  5. 微信公众平台应用开发:方法、技巧与案例--柳峰,Java语言版本

    他本人的博客:http://blog.csdn.net/lyq8479 作者简介: 刘运强,网名“柳峰”,资深微信公众平台应用开发工程师,国内微信公众平台应用开发的先驱之一,项目经验丰富.他还是一位资 ...

  6. 四则运算程序扩展:将程序改为java语言,并允许用户输入,对输入结果进行验证

    题目 每个同学选一个方向,把程序扩展一下:1.让程序能接受用户输入答案,并判定对错.最后给出总共对/错 的数量.2.把程序变成一个网页程序,用户通过设定参数,就可以得到各种题目.3.把程序变成一个Wi ...

  7. IT兄弟连 Java语法教程 Java语言的跨平台特性

    什么是平台 Java是可以跨平台的编程语言,那么首先我们需要知道什么是平台,通常我们把CPU与操作系统的整体称为平台. CPU大家都知道,是计算机的大脑,它既负责思维运算,又负责计算机中各种零部件的命 ...

  8. 基于Java语言的IO操作(文件复制)

    public static void main(String[] args) { //获取复制开始前系统时间毫秒值 long start=System.currentTimeMillis(); //文 ...

  9. java中的IO流之文件复制

    O(∩_∩)O哈哈~ 1.综述 一门成熟的语言肯定具备的几个模块:IO,通信,线程,UI...... Java作为一门成熟的程序语言,其IO流是比较复杂的.上个图大家感受下: 简单分析一下,IO分为两 ...

随机推荐

  1. password加密问题

    password加密问题 个人信息:就读于燕大本科软件project专业 眼下大三; 本人博客:google搜索"cqs_2012"就可以; 个人爱好:酷爱数据结构和算法,希望将来 ...

  2. Python 实现的下载op海贼王网的图片(网络爬虫)

    没得事就爬一下我喜欢的海贼王上的图片 须要在d盘下建立一个imgcache目录 # -*- coding: utf-8 -*- import urllib import urllib2 import ...

  3. ipv6加英文的中括号访问

    加英文的中括号就可以,如[2001:4998:c:e33::1004],我发现这是yahoo首页.但并不是所有IPv6网站都可以通过IPv6地址访问,跟IPv4一样,网站服务器端可以只绑定域名,不接受 ...

  4. Wix学习整理(1)——快速入门HelloWorld

    原文:Wix学习整理(1)--快速入门HelloWorld 1 Wix简介 Wix是Windows Installer XML的简称,其通过类XML文件格式来指定了用于创建Windows Instal ...

  5. linux上svn连接visual svn server时ssl鉴权失败,问题解决(转)

    场景:1.在windows 7上安装了visual svn server作为自己的svn服务器. 2.在虚拟机centos 6.3上使用svn客户端check代码,报错: [plain] view p ...

  6. PostgreSQL服务端监听设置及client连接方法

    背景介绍: PostgreSQL服务端执行在RedHat Linux上,IP为:192.168.230.128 client安装在Windows XP上, IP为:192.168.230.1 配置方法 ...

  7. Linux Server

    Linux Server CentOS 6.3下配置iSCSI网络存储 摘要: 一.简介iSCSI(internet SCSI)技术由IBM公司研究开发,是一个供硬件设备使用的.可以在IP协议的上层运 ...

  8. NLP 苏图南 打破自我设限 突破自我—在线播放—优酷网,视频高清在线观看

    http://v.youku.com/v_show/id_XNTAyNDg3MTky.html?x

  9. MySQL将表a中查询的数据插入到表b中

    MySQL将表a中查询的数据插入到表b中 假设表b存在 insert into b select * from a; 假设表b不存在 create table b as select * from a ...

  10. U10vim程序编辑器

    vim需要多加练习. 1.你可以将vim视为vi的高级版本.vi分成三种模式:一般模式,编辑模式和命令行模式. 一般模式:以vi打开一个文件就直接进入一般模式了(这也是默认的模式).在这个模式中,你可 ...