Java IO流复制文件详解
在Java编程中,文件操作是常见的需求之一,使用Java的IO流(Input/Output Stream)可以高效地处理文件读取和写入操作,本文将详细介绍如何使用Java IO流来复制文件,包括基本的文件复制方法以及一些高级技巧。

基本文件复制方法
最简单的文件复制方法是通过使用FileInputStream和FileOutputStream类,以下是一个基本的文件复制示例:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopyExample {
public static void main(String[] args) {
String sourceFile = "source.txt";
String destFile = "destination.txt";
try (FileInputStream fis = new FileInputStream(sourceFile);
FileOutputStream fos = new FileOutputStream(destFile)) {
int content;
while ((content = fis.read()) != -1) {
fos.write(content);
}
System.out.println("文件复制成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
在这个例子中,我们首先创建了一个FileInputStream对象来读取源文件,然后创建了一个FileOutputStream对象来写入目标文件,通过循环读取源文件的每个字节,并将其写入目标文件,直到读取到文件末尾。
使用缓冲区提高效率
在上述基本方法中,每次只读取一个字节,这在处理大文件时效率较低,为了提高效率,我们可以使用缓冲区来批量读取和写入数据。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedFileCopyExample {
public static void main(String[] args) {
String sourceFile = "source.txt";
String destFile = "destination.txt";
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile))) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
System.out.println("文件复制成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
在这个例子中,我们使用了BufferedInputStream和BufferedOutputStream来添加缓冲区,我们定义了一个1024字节的缓冲区,然后通过循环读取和写入缓冲区中的数据,这样可以显著提高文件复制的速度。
复制目录
如果需要复制整个目录,可以使用File类和递归方法来实现。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class DirectoryCopyExample {
public static void main(String[] args) {
String sourceDir = "sourceDir";
String destDir = "destinationDir";
copyDirectory(new File(sourceDir), new File(destDir));
System.out.println("目录复制成功!");
}
public static void copyDirectory(File sourceDir, File destDir) throws IOException {
if (!destDir.exists()) {
destDir.mkdir();
}
File[] files = sourceDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
copyDirectory(file, new File(destDir, file.getName()));
} else {
copyFile(file, new File(destDir, file.getName()));
}
}
}
}
public static void copyFile(File sourceFile, File destFile) throws IOException {
try (FileInputStream fis = new FileInputStream(sourceFile);
FileOutputStream fos = new FileOutputStream(destFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
}
}
}
在这个例子中,我们首先检查目标目录是否存在,如果不存在则创建它,我们遍历源目录中的所有文件和子目录,对于每个文件,如果是目录则递归调用copyDirectory方法,如果是文件则调用copyFile方法进行复制。

通过使用Java IO流,我们可以轻松地复制文件和目录,本文介绍了基本的文件复制方法、使用缓冲区提高效率以及复制整个目录的方法,掌握这些技巧可以帮助我们在Java编程中高效地处理文件操作。



















