Trying to grab filenames in a directory

C

Cindy Lamma

Hello guys, I'm an old java programmer coming back on the scene, though I
never was a java expert.



I started working on a small project which works like a database. I want to
allow users to select files and then have the DB import them. I have it set
so the user can use JfileChooser to import a specific selected file into the
database.



Now here is the problem, 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, that scans all
filenames in a directory, 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.



Thanks in advance.
 
A

Andrew Thompson

Cindy said:
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.
 
C

christian.bongiorno

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.isDirectory())
retVal.addAll(getFiles(files));
else
retVal.add(files);
}
return retVal;
}
 
T

Thomas Weidenfeller

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();
File[] files = root.listFiles();
for(int i = 0; i < files.length; i++) {
if(files.isDirectory())
retVal.addAll(getFiles(files));
else
retVal.add(files);
}
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top