• 递归遍历磁盘下的某一文件夹中所有文件,并copy文件生成文件和带文件夹的文件


    package com.hudong.test;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import org.apache.commons.io.FileUtils;
    
    public class ErgodicFile {
    
        public static void main(String[] args) throws IOException {
            File file = new File("E:\ershouok1");
            // ergodicFile(file, 0);
            ergodicFileFolder(file);
    
        }
    
        /**
         * 生成文件
         * 
         * @param file
         * @param temp
         * @return
         * @throws IOException
         */
        public static List<File> ergodicFile(File file, int temp) throws IOException {
            List<File> list = new ArrayList<File>();
            File[] fileList = file.listFiles();
    
            for (int i = 0; i < fileList.length; i++) {
    
                File docFile = new File("E:\39yiyuan\doc\" + temp + ".xml");
                File summaryFile = new File("E:\39yiyuan\summary\" + temp + ".xml");
                File contentFile = new File("E:\39yiyuan\content\" + temp + ".xml");
    
                if (fileList[i].isFile()) {   // 判断是文件
                    if ("doc.xml".equals(fileList[i].getName())) {
                        FileUtils.copyFile(fileList[i], docFile);   //copy文件
                    } else if ("summary.xml".equals(fileList[i].getName())) {
                        FileUtils.copyFile(fileList[i], summaryFile);
                    } else if ("content.xml".equals(fileList[i].getName())) {
                        FileUtils.copyFile(fileList[i], contentFile);
                    }
                } else if (fileList[i].isDirectory()) { // 判断是目录
                    ergodicFile(fileList[i], i); // 递归
                }
            }
            return list;
        }
    
        /**
         * 生成带文件夹的文件
         * 
         * @param file
         * @param temp
         * @return
         * @throws IOException
         */
        public static List<File> ergodicFileFolder(File file) throws IOException {
            List<File> list = new ArrayList<File>();
            File[] fileList = file.listFiles();
    
            for (int i = 0; i < fileList.length; i++) {   //遍历文件
    
                if (fileList[i].isFile()) {  // 判断是文件
                    if ("doc.xml".equals(fileList[i].getName())) {
                        File docFile = new File("E:/yiyuan/doc/" + System.currentTimeMillis());
                        docFile.mkdir();
                        FileUtils.copyFile(fileList[i], new File(docFile.getAbsolutePath() + "/doc.xml"));
                    } else if ("summary.xml".equals(fileList[i].getName())) {
                        File contentFile = new File("E:/yiyuan/summary/" + System.currentTimeMillis());
                        contentFile.mkdir();
                        FileUtils.copyFile(fileList[i], new File(contentFile.getAbsolutePath() + "/summary.xml"));
                    } else if ("content.xml".equals(fileList[i].getName())) {
                        File summaryFile = new File("E:/yiyuan/content/" + System.currentTimeMillis());
                        summaryFile.mkdir();
                        FileUtils.copyFile(fileList[i], new File(summaryFile.getAbsolutePath() + "/content.xml"));
                    }
                } else if (fileList[i].isDirectory()) { // 判断是目录
                    ergodicFileFolder(fileList[i]); // 递归
                }
            }
            return list;
        }
    }
    
    


     

  • 相关阅读:
    Weebly轻松创建个人网站
    人生如游戏
    1 欧拉角与四元数计算机动画笔记
    图形学相关的一些数学基础书
    1047 Student List for Course (25 分)
    1124 Raffle for Weibo Followers (20 分)
    1065 A+B and C (64bit) (20 分)
    1036 Boys vs Girls (25 分)
    1039 Course List for Student (25 分)
    1054 The Dominant Color (20 分)
  • 原文地址:https://www.cnblogs.com/yangkai-cn/p/4016803.html
Copyright © 2020-2023  润新知