package com.heima.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Demo4_CopyOther {
/**
* 把文件从其他路径,拷贝到当前路径
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file = getFile();
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file.getName());
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
fis.close();
fos.close();
}
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;
}
}
}
}
|
|