Java 文件复制 – 用 Java 复制文件的 4 种方法
原文: https://howtodoinjava.com/java/io/4-ways-to-copy-files-in-java/
在 Java 中将文件从一个位置复制到另一个位置是您在应用程序中需要执行的常见任务。 在此示例中,我列出了 4 种不同的方式来复制 Java 中的文件。 使用最适合您需要的一种。
1)使用 apache commons IO 复制文件
要使用此方法,您需要下载 apache 通用 IO 的 jar 文件,并将其作为依赖项包含在您的项目中。
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()
复制文件
这种方法非常快速且易于编写。
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()
复制文件
如果您喜欢通道类的出色表现,请使用此方法。
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 版本感到震惊,那么这个适合您。
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();
}
完整的示例代码
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);
}
}
祝您学习愉快!