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


     

  • 相关阅读:
    EF 简单的 CRUD、分页 代码笔记
    C#中的正则 Regex类
    动态字符串 Stringbuilder类
    C# 字符串操作
    集合简单总结 ArrayList、List、Hashtable、Dictionary
    C#面向对象2 静态类、静态成员的理解
    C#面向对象1 类 以及 类的继承(new、ovverride)
    无法从命令行或调试器启动服务,必须首先安装Windows服务(使用installutil.exe),然后用ServerExplorer、Windows服务器管理工具或NET START命令启动它
    WebClient上传音频文件
    发布网站详细步骤(.Net)
  • 原文地址:https://www.cnblogs.com/yangkai-cn/p/4016803.html
Copyright © 2011-2022 走看看