Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Trying to grab filenames in a directory

Thread view: 
Cindy Lamma - 11 Aug 2006 01:41 GMT
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.
Andrew Thompson - 11 Aug 2006 02:08 GMT
> 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/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.