如何实现把一个目录的文件全读出来,然后把所有文件移到另外一个目录。

解决方案 »

  1.   

    /**
         * Copy directories or files or together with files to another directory.
         * @param from it's a file name or directory name.
         * @param to it's a directory name.
         * @throws IOException the IOException.
         */
        public void copy(File from, File to) throws IOException {
            if (from.isDirectory()) {
                to.mkdir();
                File[] flist = from.listFiles();
                for (int i = 0; i < flist.length; i++) {
                    if (flist[i].isFile())
                        copy(flist[i], to);
                    else
                        copy(flist[i], new File(to.getPath() + File.separator
                            + flist[i].getName()));
                }
            } else {
                byte[] buffer = new byte[1024];
                FileInputStream fis = new FileInputStream(from);
                FileOutputStream fos = new FileOutputStream(to.getPath()
                        + File.separator + from.getName());
                int length = 0;
                while ((length = fis.read(buffer)) != -1) {
                    fos.write(buffer, 0, length);
                }
                fis.close();
                fos.close();
            }
        }