package MONA.demo03_练习题;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) throws Exception {
File file = getFile();
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file.getName());
byte[] bytes = new byte[10];
int count = 0;
while ((count = fis.read(bytes))!= -1){
fos.write(bytes,0,count);
}
fos.close();
fis.close();
System.out.println("复制完毕");
}
/**
* 获取文件
* @return
*/
public static File getFile(){
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请输入一个文件的路径");
String path = sc.next();
File file = new File(path);
if(!file.exists()){
System.out.println("输入的路径不存在");
}else if(file.isDirectory()){
System.out.println("路径必须是文件");
}else{
return file;
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)