Hi,
this is chow
I have one question...
I want to be able to read a folder structure, and convert the names of files and subfolders in to an xml file...how can we do that in java...
suggestions needed from all of you....
Printable View
Hi,
this is chow
I have one question...
I want to be able to read a folder structure, and convert the names of files and subfolders in to an xml file...how can we do that in java...
suggestions needed from all of you....
Welcome to Java Programming Forums Chow :)
It's reletively simple in java, have a look at the File class, and listFiles();
File (Java Platform SE 6)
Chris
Hello and welcome to the forums.
Please take a look here - Java Code Snippets and Tutorials - Java Programming Forums
There are lots of code examples there.
I have edited one of the examples to suit your requirements. It needs more work but the basics are there :)
Code Java:import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; /** * JavaProgrammingForums.com * Spread the word! */ public class ListFiles { public static void main(String[] args) throws IOException { Writer output = null; File file = new File("myFile.xml"); output = new BufferedWriter(new FileWriter(file)); // Directory path here String path = "C://"; String newLine = System.getProperty("line.separator"); String files = null; File folder = new File(path); File[] listOfFiles = folder.listFiles(); output.write("<?xml version='1.0'?>"); output.write(newLine); output.write("<PATH>"); output.write(newLine); System.out.println("PATH= "+ path); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isDirectory()) { files = listOfFiles[i].getName(); output.write(" <DIRECTORY>" + files + "</DIRECTORY>"); output.write(newLine); System.out.println(" <DIRECTORY>" + files + "</DIRECTORY>"); } else { files = listOfFiles[i].getName(); output.write(" <FILE>" + files + "</FILE>"); output.write(newLine); System.out.println(" <FILE>" + files + "</FILE>"); } } output.write("</PATH>"); output.close(); System.out.println("XML File Written"); } }
Thanks for your Valuable suggestions.