Created
August 27, 2010 09:16
-
-
Save joker1007/553085 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileWriter; | |
import java.io.FilenameFilter; | |
import java.io.IOException; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import org.dom4j.Document; | |
import org.dom4j.DocumentHelper; | |
import org.dom4j.Element; | |
import org.dom4j.io.OutputFormat; | |
import org.dom4j.io.XMLWriter; | |
public class SampleProgram2 { | |
/** | |
* @param args | |
* @throws FileNotFoundException | |
*/ | |
public static void main(String[] args) throws FileNotFoundException, IOException { | |
String filename = args[0]; | |
File file = new File(filename); | |
TargetFile target = new TargetFile(file); | |
File f = new File("C:\\"); | |
FilenameFilter filter = new FilenameFilter() { | |
@Override | |
public boolean accept(File dir, String name) { | |
String regexp = ".*\\.txt"; | |
Pattern p = Pattern.compile(regexp); | |
Matcher m = p.matcher(name); | |
return m.find(); | |
} | |
}; | |
File[] list = f.listFiles(filter); | |
for (File fi : list) { | |
System.out.println(fi.getAbsolutePath()); | |
} | |
target.getFileContent(); | |
Document doc = DocumentHelper.createDocument(); | |
Element add = DocumentHelper.createElement("add"); | |
Element addDoc = DocumentHelper.createElement("doc"); | |
Element pathField = DocumentHelper.createElement("field").addAttribute("name", "path").addText(target.getFilepath()); | |
Element nameField = DocumentHelper.createElement("field").addAttribute("name", "filename").addText(target.getFilename()); | |
Element sizeField = DocumentHelper.createElement("field").addAttribute("name", "size").addText(String.valueOf(target.getSize())); | |
Element mtimeField = DocumentHelper.createElement("field").addAttribute("name", "mtime").addText(String.valueOf(target.getMtime())); | |
Element textField = DocumentHelper.createElement("field").addAttribute("name", "text").addText(target.getContentText()); | |
OutputFormat outputFormat = new OutputFormat("\t", true, "UTF-8"); | |
addDoc.add(nameField); | |
addDoc.add(pathField); | |
addDoc.add(sizeField); | |
addDoc.add(mtimeField); | |
addDoc.add(textField); | |
add.add(addDoc); | |
doc.add(add); | |
System.out.println(doc.asXML()); | |
XMLWriter xw = new XMLWriter(new FileWriter("C:\\text.xml"), outputFormat); | |
xw.write(doc); | |
xw.flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment