今天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. Matlab---串口操作---数据採集篇

    matlab功能强大,串口操作也非常easy.相信看过下面两个实验你就能掌握咯! 開始吧! 实验1: 从电脑COM2口读取数据.并将数据保存在TXT文件里,方便数据分析,以下是M脚本: %名 称:Ma ...

  2. Devstack: A copy of worked local.conf I'm sharing with you.

    service_plugins = neutron.services.firewall.fwaas_plugin.FirewallPlugin [service_providers] service_ ...

  3. "ScrollView can host only one direct child"问题解决了

    1. 问题叙述性说明: (请注意以下几点大胆). ScrollView作为顶层view时报错,直接导致apk崩溃.具体错误信息例如以下: 12-21 09:12:15.150: D/AndroidRu ...

  4. 内存分析工具 MAT 的使用

    1 内存泄漏的排查方法 Dalvik Debug Monitor Server (DDMS) 是 ADT插件的一部分,当中有两项功能可用于内存检查 : ·    heap 查看堆的分配情况 ·     ...

  5. 利用python 提取log 文件里的关键句子,并进行统计分析

    利用python开发了一个提取sim.log 中的各个关键步骤中的时间并进行统计的程序: #!/usr/bin/python2.6 import re,datetime file_name='/hom ...

  6. 记得有一个奇怪的ORA-04028: cannot generate diana for object

    开发商称新一package,目前已经在翻译过程中的一些错误.提示PL/SQL:ORA-00942: table or view does not exists.这是一个非常明显的错误,即要么是表不存在 ...

  7. EF一次请求公用一个实例

    应用场景: 我们在程序开发时,对数据库的操作是必不可少的部分,常规的做法是直接使用Using()语句块,在用完后立即释放连接资源,这种做法在桌面应用程序中毫无问题,但是在Web程序中,尤其是在当今大数 ...

  8. hdu1025 Constructing Roads In JGShining's Kingdom (nlogn的LIS)

    题目链接 第一次写nlogn复杂度的LIS,纪念一下. 题目意思是说.有两条平行线.两条平行线都有n个城市,都是从左到右标记为1--n,一条线上是富有城市,一个是贫穷城市.输入n.接下来有n行,p,r ...

  9. kafka解释三的具体:发展Kafka应用

    一个.整体外观Kafka 我们知道.Kafka系统有三大组件:Producer.Consumer.broker . producers 生产(produce)消息(message)并推(push)送给 ...

  10. bellman_ford算法

    给定一个源点,求最短路径,那么存在以源点为根的最短路径树因为最短路径具有最优子结构的性质,所以我们可以先求出树的第一层,然后再求出树的第二层,以此类推bellman_ford算法就是按照这种思想求最短 ...