?
Solved

Code to store filename from folder X to a XML file

Posted on 2005-03-23
6
Medium Priority
?
174 Views
Last Modified: 2010-03-31
Hello all

My problem is that I need to store all file names that is in folder X(preferebly files in subfolders aswell) to a XML file.

So I need some procedure or method that will look through all files in a folder and insert that filename in a String, which then can be stored in a XML file.

--
Thanks for all potential help I can get ;)
0
Comment
Question by:Idarhillgaar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 15

Accepted Solution

by:
aozarov earned 500 total points
ID: 13617125
import java.io.*;

public class ListFiles
{
      public static void main(String st[]) throws Exception
      {
            StringBuffer stBuff = new StringBuffer();
            listFiles(new File(st[0]), stBuff);
            System.out.println(stBuff);
      }

      public static void listFiles(File from, StringBuffer stBuff) throws IOException
      {
            if (from.isDirectory())
            {
                  File[] files = from.listFiles();
                  for (int i = 0; i < files.length; i++)
                        listFiles(files[i], stBuff);

                  return;
            }
            
            stBuff.append(from.getPath()).append('\n');
      }
}
0
 

Author Comment

by:Idarhillgaar
ID: 13645120
Hello

I get an exception error when I'm parsing this code into my builder.

'Exception in thread main java.lang.ArrayIndexOutOfBoundsException: 0
at ListFiles.main(ListFiles.java:8)'

--
I don't really have the insight to correct this code, and how would I set the directory to whatever I want?

thanks again.
Idar
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13645843
You get the exception because this program requires the directory to list as an argument.
Hence when you run it you should do something like:
java ListFiles <here_you_should_put_the_directory_path_you_want_to_list>
If you don't want to pass this directory as a command line argument then  you can replace
listFiles(new File(st[0]), stBuff);
with
listFiles(new File(<here_you_should_put_the_directory_path_you_want_to_list>), stBuff);

Basically st[0] get replaced with the first argument in the command line. (and you get the exception if you provide none).
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Idarhillgaar
ID: 13645975
I'll get right to it, thanks for your answers.

--
Idar
0
 

Author Comment

by:Idarhillgaar
ID: 13646019
It worked like a dream, thanks again.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13646035
:-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question