遍历文件夹生成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/了解到。

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

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如下

- -


最后,整个程序如下

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

你可能感兴趣的:(Java笔记)