虽然忙着准备组成原理的实验,还是有闲暇的时间写点小程序。这次写了一个遍历文件夹中的文件和文件夹,以层次结构生成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/了解到。
下面我们第一步创建文档,根节点是
Document Doc; Element root = new Element("Source"); Doc = new Document(root);
接下来我们按层次遍历文件目录树,这里用到了栈
int count; File tempFile; DirectoryElement tempDirectory; Stack fileStack = new Stack(); Stack directoryElements = new Stack(); 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如下
- - Fiddler2 ExecAction.exe Fiddler.exe fiddler.exe.config ForceCPU.exe IE_Toolbar.ico LoadScript.wav makecert.exe saz.ico uninst.exe Xceed.Compression.dll Xceed.Compression.Formats.dll Xceed.FileSystem.dll Xceed.Zip.dll - Scripts SampleRules.js SimpleFilter.dll Timeline.dll - VSWebtest FiddlerWebTestPlugins.dll - ResponseTemplates 200_FiddlerGif.dat 200_SimpleHTML.dat 200_TransPixel.dat 204_NoContent.dat 302_Redirect.dat 303_RedirectWithGet.dat 304_NotModified.dat 307_RedirectWithMethod.dat 401_AuthBasic.dat 401_AuthDigest.dat 403_AuthDeny.dat 404_Plain.dat 407_ProxyAuthBasic.dat 502_Unreachable.dat - Inspectors Be.Windows.Forms.HexBox.dll Samples.dll Standard.dll - FiddlerHook chrome.manifest install.rdf - skin overlay.css toolbar-button.png - locale - en-US about.dtd fiddlerhook.dtd fiddlerhook.properties - defaults - preferences fiddlerhook.js - Content about.xul fiddlerhook.png firefoxOverlay.xul overlay.js
最后,整个程序如下
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 fileStack = new Stack(); Stack directoryElements = new Stack(); 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()); } } }