java复制文件的4种方式
尽管Java提供了一个可以处理文件的IO操作类。
但是没有一个复制文件的方法。
复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候。
然而有几种方法可以进行Java文件复制操作,下面列举出4中最受欢迎的方式。
###1. 使用FileStreams复制
这是最经典的方式将一个文件的内容复制到另一个文件中。
使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B。
这是第一个方法的代码:
privatestaticvoid copyFileUsingFileStreams(File source,File dest)throwsIOException{InputStream input =null;OutputStream output =null;try{
input =newFileInputStream(source);
output =newFileOutputStream(dest);byte[] buf =newbyte[1024];int bytesRead;while((bytesRead = input.read(buf))>0){
output.write(buf,0, bytesRead);}}finally{
input.close();
output.close();}}
正如你所看到的我们执行几个读和写操作try的数据,所以这应该是一个低效率的,下一个方法我们将看到新的方式。
###2. 使用FileChannel复制
Java NIO包括transferFrom方法,根据文档应该比文件流复制的速度更快。
这是第二种方法的代码:
privatestaticvoid copyFileUsingFileChannels(File source,File dest)throwsIOException{FileChannel inputChannel =null;FileChannel outputChannel =null;try{
inputChannel =newFileInputStream(source).getChannel();
outputChannel =newFileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel,0, inputChannel.size());}finally{
inputChannel.close();
outputChannel.close();}}
###3. 使用Commons IO复制
Apache Commons IO提供拷贝文件方法在其FileUtils类,可用于复制一个文件到另一个地方。它非常方便使用Apache Commons FileUtils类时,您已经使用您的项目。基本上,这个类使用Java NIO FileChannel内部。
这是第三种方法的代码:
private static void copyFileUsingApacheCommonsIO(File source,File dest)
throws IOException{FileUtils.copyFile(source, dest);}
###4. 使用Java7的Files类复制
如果你有一些经验在Java 7中你可能会知道,可以使用复制方法的Files类文件,从一个文件复制到另一个文件。
这是第四个方法的代码:
private static void copyFileUsingJava7Files(File source,File dest)
throws IOException{Files.copy(source.toPath(), dest.toPath());}
###测试
现在看到这些方法中的哪一个是更高效的,我们会复制一个大文件使用每一个在一个简单的程序。
从缓存来避免任何性能明显我们将使用四个不同的源文件和四种不同的目标文件。
让我们看一下代码:
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.nio.channels.FileChannel;import java.nio.file.Files;import org.apache.commons.io.FileUtils;publicclassCopyFilesExample{publicstaticvoid main(String[] args)throwsInterruptedException,IOException{File source =newFile("C:\\Users\\nikos7\\Desktop\\files\\sourcefile1.txt");File dest =newFile("C:\\Users\\nikos7\\Desktop\\files\\destfile1.txt");// copy file using FileStreamslong start =System.nanoTime();long end;
copyFileUsingFileStreams(source, dest);System.out.println("Time taken by FileStreams Copy = "+(System.nanoTime()- start));// copy files using java.nio.FileChannel
source =newFile("C:\\Users\\nikos7\\Desktop\\files\\sourcefile2.txt");
dest =newFile("C:\\Users\\nikos7\\Desktop\\files\\destfile2.txt");
start =System.nanoTime();
copyFileUsingFileChannels(source, dest);
end =System.nanoTime();System.out.println("Time taken by FileChannels Copy = "+(end - start));// copy file using Java 7 Files class
source =newFile("C:\\Users\\nikos7\\Desktop\\files\\sourcefile3.txt");
dest =newFile("C:\\Users\\nikos7\\Desktop\\files\\destfile3.txt");
start =System.nanoTime();
copyFileUsingJava7Files(source, dest);
end =System.nanoTime();System.out.println("Time taken by Java7 Files Copy = "+(end - start));// copy files using apache commons io
source =newFile("C:\\Users\\nikos7\\Desktop\\files\\sourcefile4.txt");
dest =newFile("C:\\Users\\nikos7\\Desktop\\files\\destfile4.txt");
start =System.nanoTime();
copyFileUsingApacheCommonsIO(source, dest);
end =System.nanoTime();System.out.println("Time taken by Apache Commons IO Copy = "+(end - start));}privatestaticvoid copyFileUsingFileStreams(File source,File dest)throwsIOException{InputStream input =null;OutputStream output =null;try{
input =newFileInputStream(source);
output =newFileOutputStream(dest);byte[] buf =newbyte[1024];int bytesRead;while((bytesRead = input.read(buf))>0){
output.write(buf,0, bytesRead);}}finally{
input.close();
output.close();}}privatestaticvoid copyFileUsingFileChannels(File source,File dest)throwsIOException{FileChannel inputChannel =null;FileChannel outputChannel =null;try{
inputChannel =newFileInputStream(source).getChannel();
outputChannel =newFileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel,0, inputChannel.size());}finally{
inputChannel.close();
outputChannel.close();}}privatestaticvoid copyFileUsingJava7Files(File source,File dest)throwsIOException{Files.copy(source.toPath(), dest.toPath());}privatestaticvoid copyFileUsingApacheCommonsIO(File source,File dest)throwsIOException{FileUtils.copyFile(source, dest);}}
####输出:
Time taken by FileStreamsCopy=127572360Time taken by FileChannelsCopy=10449963Time taken by Java7FilesCopy=10808333Time taken by ApacheCommons IO Copy=17971677
正如您可以看到的FileChannels拷贝大文件是最好的方法。如果你处理更大的文件,你会注意到一个更大的速度差。
这是一个示例,该示例演示了Java中四种不同的方法可以复制一个文件。
java复制文件的4种方式的更多相关文章
- [JAVA]java复制文件的4种方式
尽管Java提供了一个可以处理文件的IO操作类. 但是没有一个复制文件的方法. 复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候. 然而有几种方法可以进行Java文件复制操作,下面列举出 ...
- 【文件下载】Java下载文件的几种方式
[文件下载]Java下载文件的几种方式 摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...
- Java读写文件的几种方式
自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...
- java追加文件的几种方式
import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.FileWriter; import ja ...
- IO之复制文件的四种方式
1. 使用FileStreams复制 这是最经典的方式将一个文件的内容复制到另一个文件中. 使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B. 这是 ...
- Java读取文件的几种方式
package com.mesopotamia.test; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...
- Java下载文件的几种方式
转发自博客园Sunny的文章 1.以流的方式下载 public HttpServletResponse download(String path, HttpServletResponse respon ...
- java 下载文件的两种方式和java文件的上传
一:以网络的方式下载文件 try { // path是指欲下载的文件的路径. File file = new File(path); // 以流的形式下载文件. InputStream fis = n ...
- java遍历文件夹两种方式
1:非递归方式(有点类似二叉树的非递归遍历,采用链表来存储遍历到的文件夹,如果是文件就直接输出) public void traverseFile(String path){ File[] files ...
随机推荐
- asp.net 中使用不同的数据源绑定gridview
第一种,使用SqlDataReader绑定gridview.代码如下: public SqlDataReader bind() { SqlConnection con = new SqlConnect ...
- SQLserver中的常量与变量、判断循环语句
数据库中的变量与常量 数据库中定义变量 [起临时存储数据的作用] ---数据库中定义变量(运行时要从头到尾进行运行,从定义变量开始到赋值) --前面必须加 declare --定义变量 : de ...
- cocos2d-x实战 C++卷 学习笔记--第4章 win32平台下中文乱码问题
前言: 将GBK编码的字符串转为UTF-8编码.(通俗点说就是解决中文乱码问题) 简要介绍: 在Win32平台下通过 log 输出中文字符时,会出现中文乱码问题.同样的代码在 ios 和 Androi ...
- 学习之spring注解DI疑惑
接口定义 package com; public interface IPaly { void say(); } 接口实现类 package com; import org.springframewo ...
- OC1_银行账户类
// // BankAccount.h // OC1_银行账户类 // // Created by zhangxueming on 15/6/10. // Copyright (c) 2015年 zh ...
- 英语中的 姓氏/Surname
.Chomsky (Belarusian: Хомскі, Russian: Хомский, Ukrainian: Хомський, Hebrew: חומסקי, "from (V ...
- WPF FileFolderDialog 和弹出子窗口的一些问题
摘要:本文主要是WPF中 FileFolderDialog的相关问题,补充了关于在父窗口弹出子窗口,以及子窗口的相关属性(Data Binding)和命令绑定(Delegate Command)问题, ...
- cannot convert from '_TCHAR *' to 'char *'
Reference: Why can't convert TCHAR* to char* Need to Use Multi-Byte Character Set in project's setti ...
- sae-服务器php运行环境配置
config.yaml 语法- OPTION: ARG1 ARG2 ... - OPTION: if (CONDICTIONs) ACTION CONDITION可以是以下任意一种: 使用 == 和 ...
- Sql Server 判断表或数据库是否存在
发布:thebaby 来源:脚本学堂 [大 中 小] 本文详细介绍了,在sql server中判断数据库或表是否存在的方法,有理论有实例,有需要的朋友可以参考下,一定有帮助的.原文地址:h ...