首页 > 其他 > 详细

day 20+在控制台录入文件的路径,将文件拷贝到当前项目下

时间:2017-05-14 20:22:01      阅读:562      评论:0      收藏:0      [点我收藏+]

* 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;
}
}
}
}

day 20+在控制台录入文件的路径,将文件拷贝到当前项目下

原文:http://www.cnblogs.com/lixiaowei395659729/p/6853458.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!