A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 就是我的猫 中级黑马   /  2015-11-20 11:40  /  845 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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();
        }
}

1 个回复

倒序浏览
ToBeJeek1 来自手机 中级黑马 2015-11-20 12:54:56
沙发
高手,我也学会了,嘻嘻
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马