黑马程序员技术交流社区

标题: IO流(拷贝文件) [打印本页]

作者: 就是我的猫    时间: 2015-11-20 11:40
标题: IO流(拷贝文件)
IO流(拷贝文件)
在控制台录入文件的路径,将文件拷贝到当前项目下  : 两种方式  普通和 缓冲的
public class Test01 {
        public static void main(String[] args) throws IOException {
                System.out.println("请输入文件路径:");
                String path = new Scanner(System.in).nextLine();

                File file = new File(path);
               
                if(!file.exists()){
                        System.out.println("提示:没有这个文件");
                }else if (file.isDirectory()) {
                        System.out.println("提示:文件夹,我现在还不想拷贝你");
                }else {
                        copyFile(file);
                }
        }
       
        private static void copyFile(File file) throws IOException{
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
               
                int len;
                while ((len = bis.read()) != -1) {
                        bos.write(len);
                }
               
                bis.close();
                bos.close();
        }
}
作者: ToBeJeek1    时间: 2015-11-20 12:54
高手,我也学会了,嘻嘻




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2