java怎么复制文件?

互联网 19-12-3

java中复制文件的方法:(推荐: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()); }

更多java知识请关注java基础教程栏目。

以上就是java怎么复制文件?的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
标签: java
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:sql文件太大无法导入phpmyadmin

相关资讯