Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Traversing directories

Status
Not open for further replies.

Efa

Programmer
Jul 9, 2001
18
0
0
AU
Does anyone know how to traverse through directories in java. I just need to use some type and loop to change from directory, execute a file or group of files and then change to the next directory or sub directory, check if a certain type of file exists and execute it and so on.

Thanks very much

Aoife
 
1st part of your question: The file list

File dir = new File(directory);

That directory (String) should be in your os's notation. You can also use System.getProperty("user.dir"); to get the user's default directory. You can check if your File object really is a directory by saying...

if(dir.isDirectory()) {}

Then the files of this directory are begot using the following code:

String[] files = dir.list();
java.util.Arrays.sort(files);

2nd part of your question: The file execution

Have a look at the following: thread269-3292

The filename can be created from the dir's toString() and the filename's String representation via standard contatenation.

Hope this helps...
allow thyself to be the spark that lights the fire
haslo@haslo.ch - www.haslo.ch​
 
I've got the taversal and everthing else working except the execution of a file outside my java program.The code i'm using is:


try
{
run.exec(children);
}
catch ( IOException e)
{
System.out.println("Exception caught " + e ) ;
}
catch ( NullPointerException ee)
{
System.out.println("Null Pointer Exception caught " + ee ) ;
}


I get this error:


Exception caught java.io.IOException: CreateProcess: viewAll.py error=2


Any help would be appreciated,

Aoife
 
I've never before used the runtime - but you could gain additional information if you use e.printStackTrace() instead of your System.out.println("..." + e)...
allow thyself to be the spark that lights the fire
haslo@haslo.ch - www.haslo.ch​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top