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 / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

Directory browsing API??

Thread view: 
Jagd - 17 Mar 2004 21:08 GMT
I'm relatively new to java so this question might seem newbish. What
api's do I need to be using in order to browse through a local
directory and be able to see all of the file names that are in that
directory? I don't want a FileChooser where the user actually has to
select a file, but I do want something to where they can just select a
directory, and then from there I will read all of the existing files
in the directory.
Thanks in advance.
cg_news - 17 Mar 2004 21:23 GMT
> I'm relatively new to java so this question might seem newbish. What
> api's do I need to be using in order to browse through a local
[quoted text clipped - 4 lines]
> in the directory.
> Thanks in advance.

How about :

-- start psuedo code --
// some stuff...

JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

// get selected directory form jfc, make sure cancel was not pressed...

File myDirectory = new File(selectedFile);
if (myDirectory.isDirectory) {
       File subFiles[] = myDirectory.listFiles();      
       /* you now have a list of files AND sub-directories in the subFiles  
        * array you can check each one in turn by calling the isFile() method
        * for each File object. Alternatively, use the listFiles method with
        * a custom FileFilter object.
        */
}
-- end psuedo code --
I think that should give you an example of ONE possible way to accomplish
this.

HTH,
Carl.
Andrew Thompson - 17 Mar 2004 21:30 GMT
> ..What api's do I need to be using
> in order to browse through a local
> directory and be able to see all of
> the file names that are in that directory?

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

[ Where the 'file' - is a directory ]

>...I don't want a FileChooser where the user actually has to
> select a file, but I do want something to where they can just select a
> directory,

JFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

>..and then from there I will read all of the existing files
> in the directory.

Use File.list of the File object returned
by the JFileChooser

HTH

Signature

Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology



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.