今天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. 做SEO所要具备的四种能力

    1,不为失败找借口         既然我们选择了做SEO,那么发生网站被降权.被K是常常的事.当这样的情况发生时,大部分站长首先将责任推给百度机制,由于百度更新算法调整遭降权,不是由于他们的优化没有 ...

  2. VS2010调用VLFeat

    相比OpenCV,VLFeat的代码全是开源,并且非常重要的一点,事实上现的sift和Low的精度差点儿相同,这个团队全是码神,膜拜一下. 依照以下的网址进行安装,本人已经装上了,确实能够的. 安装參 ...

  3. loj1245(数学)

    传送门:Harmonic Number (II) 题意:求sum=n/1+n/2+n/3+...+n/n.(n<2^31) 分析:在一定的区间内n/i的值是一定的,因此要跳过这段区间来加速求解. ...

  4. HDU 5009 Paint Pearls (动态规划)

    Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...

  5. 在CodeBlocks 开发环境中配置使用OpenCV (ubuntu系统)

    CodeBlocks是一个开放源代码的全功能的跨平台C/C++集成开发环境.CodeBlocks由纯粹的C++语言开发完毕,它使用了蓍名的图形界面库wxWidgets.对于追求完美的C++程序猿,再也 ...

  6. ZTESoft 持续集成 编年史 之 持续集成建设---自主研发(总括)

    最终选择了自主研发,考虑到我们团队对java以及flex知识的储备,展示层使用夸浏览器的flex开发,后端业务层使用java. 一.方案: BEC + ZCIPAgent + ZCIPServer + ...

  7. hdu2295(重复覆盖+二分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2295 题意::一个国家有n个城市,有m个地方可以建造雷达,最多可以建K个雷达(K>=1 & ...

  8. 《火球——UML大战需求分析》(第1章 大话UML)——1.3 行为型的UML(Behavior Diagram)

    说明: <火球——UML大战需求分析>是我撰写的一本关于需求分析及UML方面的书,我将会在CSDN上为大家分享前面几章的内容,总字数在几万以上,图片有数十张.欢迎你按文章的序号顺序阅读,谢 ...

  9. php(LAMP)开发环境配置相关问题及解决办法

    相信很多像我一样初次接触到php开发的人,在配置基本的开发环境时都是一头雾水,为此小编特写下自己在安装配置php开发环境过程中遇到的一些问题,及解决办法. 1.LAMP组合,安装centons+apa ...

  10. 公司需求知识自学-Oracle的Package的作用及用法

    Oracle的Package的作用 简化应用设计.提高应用性能.实现信息隐藏.子程序重载. 1.Oracle的Package除 了把存储过程放到一堆儿以外还有没有其他的作用(好处)? 你不觉得把存储过 ...