import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Test2 {
/**
* 从控制台录入一个文件路径,将其拷贝到当前目录
* 1.Scanner
* 2.读文件
* 3.写文件
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file = getFile();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName()));
int len;
while((len = bis.read()) != -1){
bos.write(len);
}
bis.close();
bos.close();
System.out.println("拷贝完毕!");
}
public static File getFile() {
Scanner sc = new Scanner(System.in);
System.out.println("输入路径:");
while(true){
String line = sc.nextLine();
File file = new File(line);
if(!file.exists()){
System.out.println("文件路径不存在!重输:");
}else if(file.isDirectory()){
System.out.println("输入的是文件夹!重输:");
}else {
return file;
}
}
}
}