一個自動遞增生成目錄和檔案的cop檔案類

y_keven發表於2013-08-19
package com.hudong.util.orther;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 遍歷檔案
 * 
 * @Title: ErgodicFile.java
 * @Copyright: Copyright (c) 2005
 * @Description: <br>
 * <br>
 * @Company: ***
 * @Created on 2013-8-15 上午9:41:20
 * @author 楊凱
 */
public class JudgeCopyErgodicFile {

    private static int k = 1, m = 1;

    private final static Log logger = LogFactory.getLog(JudgeCopyErgodicFile.class);

    public static void main(String[] args) throws IOException {

        File file = new File("E:/公司資料/39網資料/最終xml資料");
        ergodicFolder(file);
    }

    public static void ergodicFolder(File file) throws IOException {

        File[] fileList = file.listFiles();

        for (int i = 0; i < fileList.length; i++) { // 遍歷檔案
            if (fileList[i].isFile()) { // 判斷是檔案
                // 這裡是必須的,一定要休眠,否則會導致檔案的覆蓋
                try {
                    Thread.sleep(1l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if ("summary.xml".equals(fileList[i].getName())) {
                    try {
                        List<Element> root = new SAXReader().read(new FileInputStream(fileList[i])).getRootElement().elements();
                        for (Element e : root) {
                            String content = e.element("SUMMARY_CONTENT").getTextTrim();
                            if (!"".equals(content)) {
                                File summaryFile = new File("E:/yiyuan/summary/" + k + "/");
                                summaryFile.mkdir();
                                FileUtils.copyFile(fileList[i], new File(summaryFile.getAbsolutePath() + "/" + System.currentTimeMillis() + ".xml"));
                                m++;
                                if (m > 1000) {
                                    k++;
                                    m = 1;
                                }
                            } else {
                                logger.info(e.element("DOC_TITLE").getText());
                                System.out.println(e.element("DOC_TITLE").getText());
                            }
                        }
                    } catch (DocumentException e1) {
                        e1.printStackTrace();
                    }
                } else if ("content.xml".equals(fileList[i].getName())) {
                    File contentFile = new File("E:/yiyuan/docinfo/" + k + "/" + System.currentTimeMillis());
                    contentFile.mkdir();
                    File desFile = new File(contentFile.getAbsolutePath() + "/docInfo.xml");
                    FileUtils.copyFile(fileList[i], desFile);
                    m++;
                    if (m > 1000) {
                        k++;
                        m = 1;
                    }
                }
            } else if (fileList[i].isDirectory()) { // 判斷是目錄
                ergodicFolder(fileList[i]); // 遞迴
            }
        }
    }
}


 

相關文章