您好,欢迎访问一九零五行业门户网

java复制文件的4种方实例

java复制文件的4种方法:(推荐:java视频教程)
一、使用filestreams复制
这是最经典的方式将一个文件的内容复制到另一个文件中。 使用fileinputstream读取文件a的字节,使用fileoutputstream写入到文件b。 这是第一个方法的代码:
private static void copyfileusingfilestreams(file source, file dest) throws ioexception { inputstream input = null; outputstream output = null; try { input = new fileinputstream(source); output = new fileoutputstream(dest); byte[] buf = new byte[1024]; int bytesread; while ((bytesread = input.read(buf)) != -1) { output.write(buf, 0, bytesread); } } finally { input.close(); output.close(); }}
正如你所看到的我们执行几个读和写操作try的数据,所以这应该是一个低效率的,下一个方法我们将看到新的方式。
二、使用filechannel复制
java nio包括transferfrom方法,根据文档应该比文件流复制的速度更快。 这是第二种方法的代码:
private static void copyfileusingfilechannels(file source, file dest) throws ioexception { filechannel inputchannel = null; filechannel outputchannel = null; try { inputchannel = new fileinputstream(source).getchannel(); outputchannel = new fileoutputstream(dest).getchannel(); outputchannel.transferfrom(inputchannel, 0, inputchannel.size()); } finally { inputchannel.close(); outputchannel.close(); }}
三、使用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); }
该方法的核心代码如下:
private static void docopyfile(file srcfile, file destfile, boolean preservefiledate) throws ioexception { if (destfile.exists() && destfile.isdirectory()) { throw new ioexception("destination '" + destfile + "' exists but is a directory"); } fileinputstream fis = null; fileoutputstream fos = null; filechannel input = null; filechannel output = null; try { fis = new fileinputstream(srcfile); fos = new fileoutputstream(destfile); input = fis.getchannel(); output = fos.getchannel(); long size = input.size(); long pos = 0; long count = 0; while (pos < size) { count = size - pos > file_copy_buffer_size ? file_copy_buffer_size : size - pos; pos += output.transferfrom(input, pos, count); } } finally { ioutils.closequietly(output); ioutils.closequietly(fos); ioutils.closequietly(input); ioutils.closequietly(fis); } if (srcfile.length() != destfile.length()) { throw new ioexception("failed to copy full contents from '" + srcfile + "' to '" + destfile + "'"); } if (preservefiledate) { destfile.setlastmodified(srcfile.lastmodified()); } }
由此可见,使用apache commons io复制文件的原理就是上述第二种方法:使用filechannel复制
四、使用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;public class copyfilesexample { public static void main(string[] args) throws interruptedexception, ioexception { file source = new file("c:\\users\\nikos7\\desktop\\files\\sourcefile1.txt"); file dest = new file("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.filechannelsource = new file("c:\\users\\nikos7\\desktop\\files\\sourcefile2.txt"); dest = new file("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 classsource = new file("c:\\users\\nikos7\\desktop\\files\\sourcefile3.txt"); dest = new file("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 iosource = new file("c:\\users\\nikos7\\desktop\\files\\sourcefile4.txt"); dest = new file("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)); } private static void copyfileusingfilestreams(file source, file dest) throws ioexception { inputstream input = null; outputstream output = null; try { input = new fileinputstream(source); output = new fileoutputstream(dest); byte[] buf = new byte[1024]; int bytesread; while ((bytesread = input.read(buf)) > 0) { output.write(buf, 0, bytesread); } } finally { input.close(); output.close(); } } private static void copyfileusingfilechannels(file source, file dest) throws ioexception { filechannel inputchannel = null; filechannel outputchannel = null; try { inputchannel = new fileinputstream(source).getchannel(); outputchannel = new fileoutputstream(dest).getchannel(); outputchannel.transferfrom(inputchannel, 0, inputchannel.size()); } finally { inputchannel.close(); outputchannel.close(); } } private static void copyfileusingjava7files(file source, file dest) throws ioexception { files.copy(source.topath(), dest.topath()); } private static void copyfileusingapachecommonsio(file source, file dest) throws ioexception { fileutils.copyfile(source, dest); }}
输出:
time taken by filestreams copy = 127572360time taken by filechannels copy = 10449963time taken by java7 files copy = 10808333time taken by apache commons io copy = 17971677
正如您可以看到的filechannels拷贝大文件是最好的方法。如果你处理更大的文件,你会注意到一个更大的速度差。 
更多java知识请关注java基础教程栏目。
以上就是java复制文件的4种方实例的详细内容。
其它类似信息

推荐信息