import java.io.*;
import java.util.Scanner;
public class 文件复制 {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入要复制的文件(绝对路径)");
String srcPath=scanner.next();
File file=new File(srcPath);
if(!file.isFile()){
System.out.println("错误");
return;
}
System.out.println("请输入要复制到的目标路径:绝对路径");
String destPath=scanner.next();
File destFile=new File(destPath);
if(destFile.exists()){
System.out.println("目标路径的文件已经存在");
return;
}
if(!destFile.getParentFile().exists()){
System.out.println("目标的父亲目录不存在");
return;
}
try (InputStream inputStream=new FileInputStream(srcPath);
OutputStream outputStream=new FileOutputStream(destFile)){
while (true){
byte[]bytes=new byte[1024];
int len =inputStream.read();
if(len==-1){
break;
}
outputStream.write(bytes,0,len);//由于字节数可能不足1024,但是系统有可能将其填满导致拿到一些无用的数据,
//因此设置起始位置和终止位置
}
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("复制完成");
}
}