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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

下面的程序我是将指定目录下所有内容复制到另一个目录文件中,不包括复制文件夹。但不知道为什么复制不了。谁帮忙找一下问题出在哪里?随便跟大家分享一下。



        /**
         * @param args
         * @throws Exception
         */

        static FileInputStream in = null;
        static FileOutputStream out = null;
        static BufferedInputStream buffin = null;
        static BufferedOutputStream buffout = null;

        public static void main(String[] args) throws Exception {
                File file = new File("E:\\myuser\\wen1");
                List<File> list = new ArrayList<File>();
                fileList(file, list);

                CopyFileJava(list);
        }
         
        public static void CopyFileJava(List<File> list) throws Exception {
                byte[] buff = new byte[1024];
                int len = 0;

                try {          
                        for (File file : list) {
                                String name = file.getName();
                                out = new FileOutputStream("E:\\myuser\\my\\");
                                in = new FileInputStream(file.toString());
                                buffin = new BufferedInputStream(in);
                                buffout = new BufferedOutputStream(out);
                                while ((len = buffin.read(buff)) != -1) {
                                        buffout.write(buff, 0, len);
                                }
                        }

                } catch (Exception e) {
                        throw e;
                } finally {
                        try {
                                if (buffin != null) {
                                        buffin.close();
                                }
                                if (buffout != null) {
                                        buffout.close();
                                }
                        } catch (Exception e) {
                                throw e;
                        }
                }
        }

         
        public static void fileList(File file, List<File> list) {
                File filenPath[] = file.listFiles();
                for (File path : filenPath) {
                        if (path.isDirectory()) {
                                fileList(path, list);
                        } else {
                                if(path.getName().endsWith(".java")){
                                        list.add(path);
                                }
                        }
                }
        }

2 个回复

倒序浏览
out = new FileOutputStream("E:\\myuser\\my\\");
是不是这儿的问题,这儿应该指定一个文件名;比如这样
  1. int count=0;
  2.               try {         
  3.                       for (File file : list) {
  4.                               String name = file.getName();
  5.                               count++;
  6.                               out = new FileOutputStream("E:\\my\\"+count+".txt");
复制代码
而且这儿应该
  1. while ((len = buffin.read(buff)) != -1) {
  2.                                       buffout.write(buff, 0, len);
  3.                                       buffout.flush();
  4.                               }
复制代码
这一步必须有! buffout.flush();

评分

参与人数 1技术分 +1 收起 理由
黄奕豪 + 1 赞一个!

查看全部评分

回复 使用道具 举报
  1.                     for (File file : list) {
  2.                             String name = file.getName();
  3.                             File ff = new File("E:/myuser/my/"+file.getName());//将拷贝过去的文件名加到路径的末尾
  4.                             if(!ff.exists()){
  5.                                     ff.createNewFile();
  6.                             }
  7.                             out = new FileOutputStream(ff);
  8.                             in = new FileInputStream(file.toString());
  9.                             buffin = new BufferedInputStream(in);
  10.                             buffout = new BufferedOutputStream(out);
  11.                             while ((len = buffin.read(buff)) != -1) {
  12.                                     buffout.write(buff, 0, len);
  13.                             }
  14.                             buffout.flush();//这句一定要加,及时将缓冲区中的数据写到文件中去
  15.                     }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马