Read files and directories structures
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Neo_Gigs
Posted On:   Tuesday, December 10, 2002 01:54 AM

Hi there all,


I have a project in mind to read the files and directories structures from a drive e.g. C: and put them into XML formatted file.


I already had the XML writting part settle, now the question I need to find out is how do I can read the files and directories structures?


To be more specific, I mean like reading every single files from the drive and list em.


I need some tutorials, sample codes or any article that explains the concept in Java.


Thanks


Neo

Re: Read files and directories structures

Posted By:   Lasse_Koskela  
Posted On:   Tuesday, December 10, 2002 03:24 AM

Here's something for getting started (not tested/compiled though):

/**
* Prints the file structure for the given file
*/
public void static printStructure(String filename)
{
// delegate the task to the private method with
// the default parameter value 0 for 'level'...
printStructure(filename, 0);
}

/**
* Prints the file structure for the given file
* using indentation for the specified level.
*/
private void static printStructure(String filename, int level)
{
// get a pointer to the root
File file = new File(filename);

// print some indentation characters
append(level);

// directories and real files are handled separately
if (file.isDirectory())
{
System.out.println("[" + filename + "]");

String[] children = file.list();
for (int i=0; i < children.length; i++)
{
printStructure(children[i], level+1);
}
}
else
{
// just print the file name
System.out.println(filename);
}
}

/**
* Prints indentation to System.out based on the given
* level.
*/
private static void append(int level)
{
for (int i=0; i < level; i++)
{
System.out.print(" ");
}
}
About | Sitemap | Contact