* 1,定义方法对键盘录入的路径进行判断,如果是文件就返回 2,在主方法中接收该文件 3,读和写该文件
*
*/
public class Test3 {
public static void main(String[] args) throws Exception {
File f = getFile();
BufferedInputStream inb = new BufferedInputStream(new FileInputStream(f));
BufferedOutputStream oub = new BufferedOutputStream(new FileOutputStream(f.getName()));
int b;
while ((b = inb.read()) != -1) {
oub.write(b);
}
inb.close();
oub.close();
}
public static File getFile() {
System.out.println("请输入一个文件路径:");
Scanner s = new Scanner(System.in);
while (true) {
String ss = s.nextLine();
File file = new File(ss);
if (!file.exists()) {
System.out.println("文件路径不存在");
} else if (file.isDirectory()) {
System.out.println("您录入的是文件夹路径,请重新录入:");
} else {
return file;
}
}
}
}