遍历文件夹生成XML

虽然忙着准备组成原理的实验,还是有闲暇的时间写点小程序。这次写了一个遍历文件夹中的文件和文件夹,以层次结构生成XML。


首先写了两个类继承自Element类,用于生成XML的文件节点和文件夹节点

package stwolf.hustbaidu.java.learn.filelist;

import java.io.File;
import java.util.Date;
import org.jdom.Element;

@SuppressWarnings(
"serial")
public class FileElement extends Element 
{
    
public FileElement(File file) 
    {
        
super("File");
        
this.setAttribute("AbsolutePath",file.getAbsolutePath());
        
this.setAttribute("Hidden",Boolean.toString(file.isHidden()));
        
this.setAttribute("canWrite",Boolean.toString(file.canWrite()));
        
this.setAttribute("canRead",Boolean.toString(file.canRead()));        
        
this.setAttribute("TotalSpace",Long.toString(file.length()/1024)+"KB");
        
this.setAttribute("LastModified",(new Date(file.lastModified())).toString());        
        
this.addContent(file.getName());
    }
}

package stwolf.hustbaidu.java.learn.filelist;

import java.io.File;
import java.util.Date;

import org.jdom.Element;

@SuppressWarnings(
"serial")
public class DirectoryElement extends Element 
{
    
public DirectoryElement(File file) {
        
super("Directory");        
        
this.setAttribute("AbsolutePath",file.getAbsolutePath());        
        
this.setAttribute("Hidden",Boolean.toString(file.isHidden()));
        
this.setAttribute("canWrite",Boolean.toString(file.canWrite()));
        
this.setAttribute("canRead",Boolean.toString(file.canRead()));    
        
this.setAttribute("LastModified",(new Date(file.lastModified())).toString());
        
this.addContent(file.getName());
    }
}


接下来就是生成XML了,其中我用的是JDOM,相关的信息可以从http://www.jdom.org/了解到。

下面我们第一步创建文档,根节点是<Source></Source>

Document Doc;
Element root 
= new Element("Source");
Doc 
= new Document(root);

接下来我们按层次遍历文件目录树,这里用到了栈

int count;
        File tempFile;
        DirectoryElement tempDirectory;
        Stack
<File> fileStack = new Stack<File>();
        Stack
<Element> directoryElements = new Stack<Element>();
        File files[];
        fileStack.push(rootDirectory);
        directoryElements.push(root);
        
while (!fileStack.isEmpty()) {
            count 
= 0;
            tempFile 
= fileStack.pop();
            tempDirectory 
= new DirectoryElement(tempFile);
            directoryElements.pop().addContent(tempDirectory);
            files 
= tempFile.listFiles();
            
for (int i = 0; i < files.length; i++) {
                
if (files[i].isDirectory()) {
                    fileStack.push(files[i]);
                    directoryElements.push(tempDirectory);
                } 
else {
                    tempDirectory.addContent(
new FileElement(files[i]));
                    
++count;
                }
            }
            tempDirectory.setAttribute(
"FileCount", Integer.toString(count));
        }

最后是生成和保存xml文件

        XMLOutputter XMLOut = new XMLOutputter();        
        File XMLOutput 
= new File(XMLPath);
        XMLOut.output(Doc, 
new FileOutputStream(XMLOutput));

下面是测试程序:

public static void main(String[] args) {
        
try {
            XMLWriter.write(
new File(
                    
"C:\\Program Files\\Fiddler2"),
                    
"C:\\Users\\ygui\\Desktop\\file.xml");
        } 
catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

程序生成的XML如下

  <?xml version="1.0" encoding="UTF-8" ?> 
<Source>
<Directory AbsolutePath="C:\Program Files\Fiddler2" Hidden="false" canWrite="true" canRead="true" LastModified="Fri Mar 19 23:59:54 CST 2010" FileCount="13">
  Fiddler2 
  
<File AbsolutePath="C:\Program Files\Fiddler2\ExecAction.exe" Hidden="false" canWrite="true" canRead="true" TotalSpace="40KB" LastModified="Mon Feb 25 07:34:04 CST 2008">ExecAction.exe</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\Fiddler.exe" Hidden="false" canWrite="true" canRead="true" TotalSpace="608KB" LastModified="Wed Jan 27 03:11:28 CST 2010">Fiddler.exe</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\fiddler.exe.config" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Fri Oct 16 08:32:28 CST 2009">fiddler.exe.config</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\ForceCPU.exe" Hidden="false" canWrite="true" canRead="true" TotalSpace="6KB" LastModified="Sat Oct 31 09:03:40 CST 2009">ForceCPU.exe</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\IE_Toolbar.ico" Hidden="false" canWrite="true" canRead="true" TotalSpace="3KB" LastModified="Sat Aug 02 08:38:56 CST 2008">IE_Toolbar.ico</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\LoadScript.wav" Hidden="false" canWrite="true" canRead="true" TotalSpace="2KB" LastModified="Wed May 17 14:00:04 CST 2006">LoadScript.wav</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\makecert.exe" Hidden="false" canWrite="true" canRead="true" TotalSpace="54KB" LastModified="Wed Jul 15 13:10:42 CST 2009">makecert.exe</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\saz.ico" Hidden="false" canWrite="true" canRead="true" TotalSpace="11KB" LastModified="Sat Aug 02 09:06:08 CST 2008">saz.ico</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\uninst.exe" Hidden="false" canWrite="true" canRead="true" TotalSpace="48KB" LastModified="Fri Mar 19 23:59:54 CST 2010">uninst.exe</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\Xceed.Compression.dll" Hidden="false" canWrite="true" canRead="true" TotalSpace="196KB" LastModified="Tue Aug 18 03:34:08 CST 2009">Xceed.Compression.dll</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\Xceed.Compression.Formats.dll" Hidden="false" canWrite="true" canRead="true" TotalSpace="76KB" LastModified="Tue Aug 18 03:36:52 CST 2009">Xceed.Compression.Formats.dll</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\Xceed.FileSystem.dll" Hidden="false" canWrite="true" canRead="true" TotalSpace="124KB" LastModified="Tue Aug 18 03:34:14 CST 2009">Xceed.FileSystem.dll</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\Xceed.Zip.dll" Hidden="false" canWrite="true" canRead="true" TotalSpace="244KB" LastModified="Tue Aug 18 03:37:20 CST 2009">Xceed.Zip.dll</File> 
<Directory AbsolutePath="C:\Program Files\Fiddler2\Scripts" Hidden="false" canWrite="true" canRead="true" LastModified="Fri Mar 19 23:59:54 CST 2010" FileCount="3">
  Scripts 
  
<File AbsolutePath="C:\Program Files\Fiddler2\Scripts\SampleRules.js" Hidden="false" canWrite="true" canRead="true" TotalSpace="13KB" LastModified="Tue Jan 19 02:07:36 CST 2010">SampleRules.js</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\Scripts\SimpleFilter.dll" Hidden="false" canWrite="true" canRead="true" TotalSpace="52KB" LastModified="Mon Jan 25 08:45:22 CST 2010">SimpleFilter.dll</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\Scripts\Timeline.dll" Hidden="false" canWrite="true" canRead="true" TotalSpace="37KB" LastModified="Mon Jan 25 08:45:36 CST 2010">Timeline.dll</File> 
<Directory AbsolutePath="C:\Program Files\Fiddler2\Scripts\VSWebtest" Hidden="false" canWrite="true" canRead="true" LastModified="Fri Mar 19 23:59:54 CST 2010" FileCount="1">
  VSWebtest 
  
<File AbsolutePath="C:\Program Files\Fiddler2\Scripts\VSWebtest\FiddlerWebTestPlugins.dll" Hidden="false" canWrite="true" canRead="true" TotalSpace="16KB" LastModified="Fri Jun 22 16:31:40 CST 2007">FiddlerWebTestPlugins.dll</File> 
  
</Directory>
  
</Directory>
<Directory AbsolutePath="C:\Program Files\Fiddler2\ResponseTemplates" Hidden="false" canWrite="true" canRead="true" LastModified="Fri Mar 19 23:59:54 CST 2010" FileCount="14">
  ResponseTemplates 
  
<File AbsolutePath="C:\Program Files\Fiddler2\ResponseTemplates\200_FiddlerGif.dat" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Thu Feb 14 15:40:36 CST 2008">200_FiddlerGif.dat</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\ResponseTemplates\200_SimpleHTML.dat" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Tue Feb 26 10:51:40 CST 2008">200_SimpleHTML.dat</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\ResponseTemplates\200_TransPixel.dat" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Thu Feb 14 15:46:36 CST 2008">200_TransPixel.dat</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\ResponseTemplates\204_NoContent.dat" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Thu Feb 14 15:41:30 CST 2008">204_NoContent.dat</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\ResponseTemplates\302_Redirect.dat" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Thu Feb 14 15:45:12 CST 2008">302_Redirect.dat</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\ResponseTemplates\303_RedirectWithGet.dat" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Thu Feb 14 15:44:52 CST 2008">303_RedirectWithGet.dat</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\ResponseTemplates\304_NotModified.dat" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Thu Feb 14 15:38:48 CST 2008">304_NotModified.dat</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\ResponseTemplates\307_RedirectWithMethod.dat" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Sun Feb 17 08:24:50 CST 2008">307_RedirectWithMethod.dat</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\ResponseTemplates\401_AuthBasic.dat" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Sun Feb 17 08:22:18 CST 2008">401_AuthBasic.dat</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\ResponseTemplates\401_AuthDigest.dat" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Sun Feb 17 08:22:00 CST 2008">401_AuthDigest.dat</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\ResponseTemplates\403_AuthDeny.dat" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Sun Feb 17 08:24:30 CST 2008">403_AuthDeny.dat</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\ResponseTemplates\404_Plain.dat" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Thu Feb 14 15:32:52 CST 2008">404_Plain.dat</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\ResponseTemplates\407_ProxyAuthBasic.dat" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Thu Feb 14 15:33:06 CST 2008">407_ProxyAuthBasic.dat</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\ResponseTemplates\502_Unreachable.dat" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Thu Feb 14 15:34:02 CST 2008">502_Unreachable.dat</File> 
  
</Directory>
<Directory AbsolutePath="C:\Program Files\Fiddler2\Inspectors" Hidden="false" canWrite="true" canRead="true" LastModified="Fri Mar 19 23:59:54 CST 2010" FileCount="3">
  Inspectors 
  
<File AbsolutePath="C:\Program Files\Fiddler2\Inspectors\Be.Windows.Forms.HexBox.dll" Hidden="false" canWrite="true" canRead="true" TotalSpace="60KB" LastModified="Thu Nov 05 05:46:48 CST 2009">Be.Windows.Forms.HexBox.dll</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\Inspectors\Samples.dll" Hidden="false" canWrite="true" canRead="true" TotalSpace="84KB" LastModified="Tue Jan 26 06:27:48 CST 2010">Samples.dll</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\Inspectors\Standard.dll" Hidden="false" canWrite="true" canRead="true" TotalSpace="84KB" LastModified="Wed Jan 20 01:53:20 CST 2010">Standard.dll</File> 
  
</Directory>
<Directory AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook" Hidden="false" canWrite="true" canRead="true" LastModified="Fri Mar 19 23:59:54 CST 2010" FileCount="2">
  FiddlerHook 
  
<File AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\chrome.manifest" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Wed Mar 11 04:18:04 CST 2009">chrome.manifest</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\install.rdf" Hidden="false" canWrite="true" canRead="true" TotalSpace="1KB" LastModified="Sun Jan 17 07:50:08 CST 2010">install.rdf</File> 
<Directory AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\skin" Hidden="false" canWrite="true" canRead="true" LastModified="Fri Mar 19 23:59:54 CST 2010" FileCount="2">
  skin 
  
<File AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\skin\overlay.css" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Thu Mar 12 09:51:52 CST 2009">overlay.css</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\skin\toolbar-button.png" Hidden="false" canWrite="true" canRead="true" TotalSpace="1KB" LastModified="Sat Mar 14 05:26:12 CST 2009">toolbar-button.png</File> 
  
</Directory>
<Directory AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\locale" Hidden="false" canWrite="true" canRead="true" LastModified="Fri Mar 19 23:59:54 CST 2010" FileCount="0">
  locale 
<Directory AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\locale\en-US" Hidden="false" canWrite="true" canRead="true" LastModified="Fri Mar 19 23:59:54 CST 2010" FileCount="3">
  en-US 
  
<File AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\locale\en-US\about.dtd" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Wed Mar 11 04:18:04 CST 2009">about.dtd</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\locale\en-US\fiddlerhook.dtd" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Tue Dec 22 04:32:26 CST 2009">fiddlerhook.dtd</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\locale\en-US\fiddlerhook.properties" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Thu Mar 12 09:51:24 CST 2009">fiddlerhook.properties</File> 
  
</Directory>
  
</Directory>
<Directory AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\defaults" Hidden="false" canWrite="true" canRead="true" LastModified="Fri Mar 19 23:59:54 CST 2010" FileCount="0">
  defaults 
<Directory AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\defaults\preferences" Hidden="false" canWrite="true" canRead="true" LastModified="Fri Mar 19 23:59:54 CST 2010" FileCount="1">
  preferences 
  
<File AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\defaults\preferences\fiddlerhook.js" Hidden="false" canWrite="true" canRead="true" TotalSpace="0KB" LastModified="Thu Sep 03 02:02:52 CST 2009">fiddlerhook.js</File> 
  
</Directory>
  
</Directory>
<Directory AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\Content" Hidden="false" canWrite="true" canRead="true" LastModified="Fri Mar 19 23:59:54 CST 2010" FileCount="4">
  Content 
  
<File AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\Content\about.xul" Hidden="false" canWrite="true" canRead="true" TotalSpace="1KB" LastModified="Sun Jan 17 08:48:28 CST 2010">about.xul</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\Content\fiddlerhook.png" Hidden="false" canWrite="true" canRead="true" TotalSpace="1KB" LastModified="Sat Mar 14 05:23:36 CST 2009">fiddlerhook.png</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\Content\firefoxOverlay.xul" Hidden="false" canWrite="true" canRead="true" TotalSpace="4KB" LastModified="Sun Jan 17 08:48:48 CST 2010">firefoxOverlay.xul</File> 
  
<File AbsolutePath="C:\Program Files\Fiddler2\FiddlerHook\Content\overlay.js" Hidden="false" canWrite="true" canRead="true" TotalSpace="16KB" LastModified="Sun Jan 17 09:46:04 CST 2010">overlay.js</File> 
  
</Directory>
  
</Directory>
  
</Directory>
  
</Source>


最后,整个程序如下

package stwolf.hustbaidu.java.learn.filelist;

import java.io.*;
import java.util.Stack;
import org.jdom.*;
import org.jdom.output.*;

public class XMLWriter {
    
public static void write(File rootDirectory, String XMLPath)
            
throws IOException, JDOMException {

        Document Doc;
        Element root 
= new Element("Source");
        Doc 
= new Document(root);

        
int count;
        File tempFile;
        DirectoryElement tempDirectory;
        Stack
<File> fileStack = new Stack<File>();
        Stack
<Element> directoryElements = new Stack<Element>();
        File files[];
        fileStack.push(rootDirectory);
        directoryElements.push(root);
        
while (!fileStack.isEmpty()) {
            count 
= 0;
            tempFile 
= fileStack.pop();
            tempDirectory 
= new DirectoryElement(tempFile);
            directoryElements.pop().addContent(tempDirectory);
            files 
= tempFile.listFiles();
            
for (int i = 0; i < files.length; i++) {
                
if (files[i].isDirectory()) {
                    fileStack.push(files[i]);
                    directoryElements.push(tempDirectory);
                } 
else {
                    tempDirectory.addContent(
new FileElement(files[i]));
                    
++count;
                }
            }
            tempDirectory.setAttribute(
"FileCount", Integer.toString(count));
        }

        XMLOutputter XMLOut 
= new XMLOutputter();
        
// XMLOut.setEncoding("gb2312");
        File XMLOutput = new File(XMLPath);
        XMLOut.output(Doc, 
new FileOutputStream(XMLOutput));
        System.out.println(
"XML Created!");
    }

    
public static void main(String[] args) {
        
try {
            XMLWriter.write(
new File(
                    
"C:\\Program Files\\Fiddler2"),
                    
"C:\\Users\\ygui\\Desktop\\file.xml");
        } 
catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}

posted @ 2010-03-29 23:07  狼の禅  阅读(2998)  评论(0编辑  收藏  举报
我要啦免费统计