> Hello guys, I'm an old java programmer coming back on the scene, though I
> never was a java expert.
...
> ..I want the user to also select just a directory if
> they want to, and the DB can import all the files in that dir manually.
>
> I found a method which sets JfileChooser to only allow selecting of
> directories. But NOW WHAT? I was thinking of making a loop,
Yes.
>...that scans all
> filenames in a directory,
File.listFiles()
File.listFiles(FilenameFilter)
>..then I'll have a method open each one of them in
> sequence. But this is what has me baffled, how to do this?
>
> I'm not even sure where to start, even though I can easily grab the
> directory NAME the files are in. The last time I did this sort of thing, I
> remember having to use Shell Commands.
Just how long have you been away from Java?
File.listFiles() was introduced in Java 1.2!
Andrew T.
christian.bongiorno@gmail.com - 11 Aug 2006 04:46 GMT
Hi Cindy
You are also going to have to recursively proceed into directories as
well to get further files. I mean, I assume you want to
public static void main(String[] args) {
System.out.println(getFiles(new File(args[0])));
}
private static List getFiles(File root) {
List retVal = new LinkedList();
File[] files = root.listFiles();
for(int i = 0; i < files.length; i++) {
if(files[i].isDirectory())
retVal.addAll(getFiles(files[i]));
else
retVal.add(files[i]);
}
return retVal;
}
Thomas Weidenfeller - 11 Aug 2006 10:44 GMT
> You are also going to have to recursively proceed into directories as
> well to get further files.
> private static List getFiles(File root) {
> List retVal = new LinkedList();
[quoted text clipped - 7 lines]
> return retVal;
> }
And don't forget to add a check to prevent endless recursions. Some file
systems allow to have loops in the directory hierarchy.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/