使用FileInputStream+BufferedInputStream与FileOutputStream+BufferedOutputStream实现文件复制,是很多人普遍想到的一种方法。在这里,我将给出另外一种方法,使用FileChannel通道。其实,还有一些其他方法,想了解更多,清参见我转载的文章:JAVA NIO 简介。
不多说,直接贴出源码:
srcFile和destFile:均为文件而非目录;
overlay:指示是否覆盖已存在目标文件。
package com.pluto.file;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class FileCopy {
private final int BUFFER_SIZE = 4096;
public long bufferCopyFile(File srcFile, File destFile, boolean overlay) throws FileNotFoundException {
if (!srcFile.exists()) {
throw new FileNotFoundException("At path " + srcFile.getAbsolutePath());
}
if (destFile.exists() && overlay) {
destFile.delete();
}
BufferedInputStream bufin = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bufout = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] buffer = new byte[BUFFER_SIZE];
long totalBytesReaded = 0;
int bytesReaded = -1;
try {
while ((bytesReaded = bufin.read(buffer, 0, BUFFER_SIZE)) != -1) {
bufout.write(buffer, 0, bytesReaded);
totalBytesReaded += bytesReaded;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufin.close();
bufout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return totalBytesReaded;
}
public long channelCopyFile(File srcFile, File destFile, boolean overlay) throws FileNotFoundException {
if (!srcFile.exists()) {
throw new FileNotFoundException("At path " + srcFile.getAbsolutePath());
}
if (destFile.exists() && overlay) {
destFile.delete();
}
// 获取源文件和目标文件的输入输出流
FileInputStream fin = new FileInputStream(srcFile);
FileOutputStream fout = new FileOutputStream(destFile);
// 获取输入输出通道
FileChannel fcin = fin.getChannel();
FileChannel fcout = fout.getChannel();
long totalBytesReaded = 0;
try{
totalBytesReaded = fcin.transferTo(0, fcin.size(), fcout);
}catch(IOException e){
e.printStackTrace();
} finally {
try {
fin.close();
fout.close();
fcin.close();
fcout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return totalBytesReaded;
}
}
在本机上面做了个简单的性能测试。在下面的测试源码中,beautiful.rmvb是一个600M左右的文件
FileCopy fileCopy = new FileCopy();
File srcFile = new File("/home/pluto/temporary/filecopy/beautiful.rmvb");
File destFile1 = new File("/home/pluto/temporary/filecopy/dest/b.rmvb");
File destFile2 = new File("/home/pluto/temporary/filecopy/dest/be.rmvb");
long totalBytes = 0;
long start = System.currentTimeMillis();
try {
totalBytes = fileCopy.bufferCopyFile(srcFile, destFile1, false);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("bufferCopyFile: " + totalBytes + " spend: " + (end - start));
start = System.currentTimeMillis();
try {
totalBytes = fileCopy.channelCopyFile(srcFile, destFile2, false);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
end = System.currentTimeMillis();
System.out.println("\nchannelCopyFile: " + totalBytes + " spend: " + (end - start));
运行输出结果如下:
bufferCopyFile: 685049654 spend: 26077
channelCopyFile: 685049654 spend: 28345
我的电脑性能一般,上面的结果只具有参考性。
分享到:
相关推荐
java文件复制,使用java语言开发的,效果如windows 的复制和粘贴效果。
一个简单的基于java实现的文件复制器,分享给大家学习
将文件从一个文件夹复制到指定的目录,可用于服务器文件复制,只需传入文件地址以及目录地址即可完成复制
可以复制单个文件或整个文件夹下的所有文件
实现文本复制操作的简易JAVA代码,适用于JAVA初学者
利用java实现文件的复制整个文件夹内容,实现文件的复制
这是JAVA SE中的文件复制的代码,很多人对于文件复制不是很理解,希望通过此代码可以帮助更多的人来理解怎么是用Java语言实现文件的复制
java复制文件的4种方式 java复制文件的4种方式 java复制文件的4种方式
用户输入原始文件地址。。。然后输入要复制到的目录。
1)在FileCopy类中,建立copy方法,实现文件复制的内容; 2)程序中需要考虑输入输出异常处理; 3)在dos控制台窗口环境下,通过命令行参数运行程序,如: Java FileCopy test1.txt test2.txt
Java文件复制源代码(含使用Swing组件、无法复制文件夹)
实现简单的文件复制功能,适合java入门学习 输入输出部分
Java实现多线的文件复制(界面)。 可以选择复制文件的路径,和复制到某处的路径。可以选择线程数量。
这是一个利用java swing做的一个文件复制器,你可以根据自己的需要选择复制的文件的类型,可以自己设定文件筛选的类型。反正是一款很实用的文件复制器。。相信我没错了。 注:解压密码仍然是:you'dbest
BufferedWriter BufferedReader 具体例子
本程序用java语言实现了文件流的几乎所有操作,如文件复制,移动,删除,新建等。
1)在FileCopy类中,建立copy方法,实现文件复制的内容; 2)程序中需要考虑输入输出异常处理; 3)在dos控制台窗口环境下,通过命令行参数运行程序。
java编程,使用FileInputStream ,FileOutputStream 实现了把一个文件的内容复制到另外一个文件 /* * 多种类型文件的复制 */