62,628
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) throws Exception {
String path = "/Users/Desktop/aa";
File file1 = new File(path, "1.pdf");
File file2 = new File(path, "2.pdf");
if (!file1.exists()) {
System.out.println("文件" + file1 + "不存在");
return;
}
if (file2.exists()) {
System.out.println("文件已存在,执行删除操作");
file2.delete();
}
System.out.println("开始复制" + file2);
try (InputStream in = new BufferedInputStream(new FileInputStream(file1));
OutputStream out = new BufferedOutputStream(new FileOutputStream(file2))) {
int c = 0;
int total = 0;
while (c != -1) {
c = in.read();
total += c;
out.write((char) c);
}
System.out.println("文件复制完毕共" + total + "字节");
}
}


