zoukankan      html  css  js  c++  java
  • 递归遍历磁盘下的某一文件夹中所有文件,并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;
        }
    }
    
    


     

  • 相关阅读:
    【UNIX环境高级编程】线程同步
    死锁
    Shell Script的默认变量
    高通平台读写nv总结
    PLMN概念和应用设置
    win10间歇性的找不到usb设备
    (转)查询或修改iPhone的短信服务中心号码(iOS通用)
    (转)CS域和PS域
    SSL&HTTPS简单介绍
    WAV和PCM的关系和区别
  • 原文地址:https://www.cnblogs.com/yangkai-cn/p/4016803.html
Copyright © 2011-2022 走看看