Advertisement

[java] List dirs

Started by June 05, 2000 09:44 PM
8 comments, last by Smoo 24 years, 6 months ago
Does anyone know how to list directories instead of files while using something similar to FileEnumerator? And another question, is there a method similar to FileEnumerator that doesn''t use the package com.ms.wfc.io? Thanks, Smoo.
Create a Java.io.File object...use the list() method to get an array of files (or directories)...then use isDirectory() on each record of the array to see if it is a directory or not.
Advertisement
http://java.sun.com/products/jdk/1.2/docs/api/

The API docs are your friend

java.io.File
File.isDirectory()
File.listFiles()
File.list()

You can even supply FileFilters to the list so that you only get back files of a certain type.
Thanks for the info, it''s definitely usefull. Unfortunately now I have another related problem.

File.list() returns a string[] which is great but I still have troubles finding directories out of those.

With FileEnumerator I could simply do:
while (fe.hasMoreFiles())
{
if (fe.nextFile().isDirectory)
...
}

With File.list() I think I need to use filters. Well, needless to say the help on filters isn''t very practicle.

My question is does anyone know how to create a filter to detect directories only? Perhaps there is a way to detect a file''s attributes that I am unaware of.

Thanks,
Smoo
testing...
File f1 = new File("some file or directory");
String[] fileArray = f1.list();
File f2;
for (int i=0; i < fileArray.length; i++) {
f2 = new File(fileArray);
if (f2.isDirectory())
//do what you need
}

I keep getting disconnected when I try to format the code...sorry
Advertisement
Well, I tried the bit of code and it doesn''t work.
Here''s my code snippet which is the same as the anon poster :

File file1 = new File("Images/Hero/");
File file2;

String stArray[] = file1.list();

for (int x = 0; x < stArray.length; x++)
{
file2 = new File(stArray[x]);
if (file2.isDirectory())
lsActions.add(stArray[x] + "()");
}

Everything works except the if statement returns false

Any suggestions to correct this would be appreciated,
Smoo
I think you should do it like this:

File file1 = new File("Images/Hero/"); String strBaseDir = file1.getAbsolutePath();File file2;String stArray[] = file1.list();for (int x = 0; x < stArray.length; x++){  file2 = new File( strBaseDir+File.separator+stArray[x] );  if ( file2.isDirectory() )    lsActions.add(stArray[x] + "()");  }}


You see the list() method returns the files & directories relative to the file1, but you need absolute references to get correct File objects.

Hope this helps...
-Pasi Keranen
What javanerd posted should work under any JDK...however, under JDK1.2+ the following can also be used:

File f1 = new File("whatever");
File[] fileArray = f1.listFiles();

for (int i=0; i < fileArray.length; i++)
if (fileArray.isDirectory())
//do what you need

java.io.File.list() returns a String[] array.
java.io.File.listFiles returns a File[] array.

To implement a FileFilter, i'd imagine you would have
your class implement FileFilter, then have a method
like so:

public boolean accept (File f) {
StringBuffer sb = new StringBuffer(f.toString);
if(sb.substring(sb.length()-3,sb.length()).equals("jpg") ){
return true;
} else {
return false;
}
}

Something like that i'd imagine.

Edited by - Jim_Ross on June 11, 2000 1:51:46 PM

This topic is closed to new replies.

Advertisement