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 the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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
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"); } }
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
Thanks for your Valuable suggestions.