多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Java 文件复制 – 用 Java 复制文件的 4 种方法 > 原文: [https://howtodoinjava.com/java/io/4-ways-to-copy-files-in-java/](https://howtodoinjava.com/java/io/4-ways-to-copy-files-in-java/) **在 Java** 中将文件从一个位置复制到另一个位置是您在应用程序中需要执行的常见任务。 在此示例中,我列出了 4 种不同的方式来**复制 Java** 中的文件。 使用最适合您需要的一种。 ## 1)使用 apache commons IO 复制文件 要使用此方法,您需要[下载 apache 通用 IO 的 jar](https://commons.apache.org/proper/commons-io/download_io.cgi "download apache common io") 文件,并将其作为依赖项包含在您的项目中。 ```java private static void fileCopyUsingApacheCommons() throws IOException { File fileToCopy = new File("c:/temp/testoriginal.txt"); File newFile = new File("c:/temp/testcopied.txt"); FileUtils.copyFile(fileToCopy, newFile); // OR IOUtils.copy(new FileInputStream(fileToCopy), new FileOutputStream(newFile)); } ``` ## 2)使用`java.nio.file.Files.copy()`复制文件 这种方法非常快速且易于编写。 ```java private static void fileCopyUsingNIOFilesClass() throws IOException { Path source = Paths.get("c:/temp/testoriginal.txt"); Path destination = Paths.get("c:/temp/testcopied.txt"); Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING); } ``` ## 3)使用`java.nio.channels.FileChannel.transferTo()`复制文件 如果您喜欢通道类的出色表现,请使用此方法。 ```java private static void fileCopyUsingNIOChannelClass() throws IOException { File fileToCopy = new File("c:/temp/testoriginal.txt"); FileInputStream inputStream = new FileInputStream(fileToCopy); FileChannel inChannel = inputStream.getChannel(); File newFile = new File("c:/temp/testcopied.txt"); FileOutputStream outputStream = new FileOutputStream(newFile); FileChannel outChannel = outputStream.getChannel(); inChannel.transferTo(0, fileToCopy.length(), outChannel); inputStream.close(); outputStream.close(); } ``` ## 4)使用`FileStreams`复制文件 如果您对较旧的 Java 版本感到震惊,那么这个适合您。 ```java private static void fileCopyUsingFileStreams() throws IOException { File fileToCopy = new File("c:/temp/testoriginal.txt"); FileInputStream input = new FileInputStream(fileToCopy); File newFile = new File("c:/temp/testcopied.txt"); FileOutputStream output = new FileOutputStream(newFile); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = input.read(buf)) > 0) { output.write(buf, 0, bytesRead); } input.close(); output.close(); } ``` ## 完整的示例代码 ```java package com.howtodoinjava.examples.io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; public class FileCopyExamples { public static void main(String[] args) throws IOException { fileCopyUsingApacheCommons(); fileCopyUsingNIOFilesClass(); fileCopyUsingNIOChannelClass(); fileCopyUsingFileStreams(); } private static void fileCopyUsingFileStreams() throws IOException { File fileToCopy = new File("c:/temp/testoriginal.txt"); FileInputStream input = new FileInputStream(fileToCopy); File newFile = new File("c:/temp/testcopied.txt"); FileOutputStream output = new FileOutputStream(newFile); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = input.read(buf)) > 0) { output.write(buf, 0, bytesRead); } input.close(); output.close(); } private static void fileCopyUsingNIOChannelClass() throws IOException { File fileToCopy = new File("c:/temp/testoriginal.txt"); FileInputStream inputStream = new FileInputStream(fileToCopy); FileChannel inChannel = inputStream.getChannel(); File newFile = new File("c:/temp/testcopied.txt"); FileOutputStream outputStream = new FileOutputStream(newFile); FileChannel outChannel = outputStream.getChannel(); inChannel.transferTo(0, fileToCopy.length(), outChannel); inputStream.close(); outputStream.close(); } private static void fileCopyUsingApacheCommons() throws IOException { File fileToCopy = new File("c:/temp/testoriginal.txt"); File newFile = new File("c:/temp/testcopied.txt"); FileUtils.copyFile(fileToCopy, newFile); // OR IOUtils.copy(new FileInputStream(fileToCopy), new FileOutputStream(newFile)); } private static void fileCopyUsingNIOFilesClass() throws IOException { Path source = Paths.get("c:/temp/testoriginal.txt"); Path destination = Paths.get("c:/temp/testcopied.txt"); Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING); } } ``` **祝您学习愉快!**