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.

Results 1 to 4 of 4

Thread: Hi to every one in the forum

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hi to every one in the forum

    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....


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Hi to every one in the forum

    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

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Hi to every one in the forum

    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.

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hi to every one in the forum

    Thanks for your Valuable suggestions.

Similar Threads

  1. New to forum
    By cxlizard in forum Member Introductions
    Replies: 0
    Last Post: December 19th, 2010, 04:35 PM
  2. First time in forum
    By prasanth1216 in forum Member Introductions
    Replies: 0
    Last Post: December 19th, 2010, 01:45 PM
  3. Forum downtime
    By JavaPF in forum The Cafe
    Replies: 0
    Last Post: October 11th, 2010, 10:07 AM
  4. Didn't see this forum... but hey!
    By Lord.Quackstar in forum Member Introductions
    Replies: 2
    Last Post: June 10th, 2010, 11:42 AM
  5. New to the forum
    By HaraSmoomma in forum Member Introductions
    Replies: 0
    Last Post: January 20th, 2010, 07:48 AM