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]); // 递归 } } } }