import java.io.*;
public class FileCopyExample {
public static void main(String[] args) {
// 源文件和目标文件的路径
File sourceFile = new File("源文件路径.txt");
File destFile = new File("目标文件路径.txt");
// 复制文件
try (InputStream in = new FileInputStream(sourceFile);
OutputStream out = new FileOutputStream(destFile);
BufferedInputStream bis = new BufferedInputStream(in);
BufferedOutputStream bos = new BufferedOutputStream(out)) {
byte[] buffer = new byte[1024];
int bytesRead;
// 读取和写入文件
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush(); // 确保所有数据都被写入
System.out.println("文件拷贝成功!");
} catch (IOException e) {
System.err.println("文件操作异常: " + e.getMessage());
}
}
}