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