用java写一个文件复制
时间: 2023-03-29 15:04:03 浏览: 63
程序可以这样实现:
import java.io.*;
public class FileCopy {
public static void main(String[] args) {
String sourcePath = "source.txt"; // 源文件路径
String targetPath = "target.txt"; // 目标文件路径
try {
FileInputStream fis = new FileInputStream(sourcePath);
FileOutputStream fos = new FileOutputStream(targetPath);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, , len);
}
fis.close();
fos.close();
System.out.println("文件复制成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}