package com.heima.tets;
import java.awt.image.BufferedImage;
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 Test4 {
/**
*实现需求:在控制台录入文件的路径,将文件拷贝到当前项目下
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个文件路径:");
String line = sc.nextLine();
File file = new File(line);//封装成file对象
//判断
if(!file.exists()){
System.out.println("您录入的路径不存在");
}else if(file.isDirectory()){
System.out.println("您录入的是文件夹,请重新录入");
}
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));//创建输入流
//创建输出流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName()));
int b;
while((b=bis.read())!=-1){
bos.write(b);
}
bis.close();
bos.close();
}
}
|
|