0

I need some advice on trying to figure out why when I use the list or listFiles method it doesn't return a list of subdirectories that are in the parent directory. When I run the program I choose a file and after passing the file(current directory) to a string array it should iterate through and give me all of the subdirectories as well, but it isn't. I also tried just dealing in File class by using listFiles(), but no luck. Can anyone give me any pointers and why the list method isn't returning the sub directories as well.

    public class jFileChooser {

    public void browse(){

    File f = null;
    String[] paths;

    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);


    if(chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){

        f = chooser.getCurrentDirectory();
        paths = f.list();
        for(String i:paths){
            System.out.println(i);
        }


    }


}
    public static void main(String[] args){

        jFileChooser choser = new jFileChooser();
        choser.browse();
    }
}
javanewb
  • 35
  • 1
  • 1
  • 6
  • It works fine for me. Are you sure you want to use `chooser.getCurrentDirectory();` instead of `chooser.getSelectedFile();`? – Pshemo Sep 20 '15 at 18:06
  • Pshemo… hmm…that's weird. It returns all sub directories for you? I tried using getSelectFile() but when I run it returns a null pointer exception. – javanewb Sep 20 '15 at 18:51
  • `getCurrentDirectory` returns directory which *contains* selected element, so if you selected `c:\foo\bar` it will return `c:\foo`. And when I iterate I see `bar` and rest of elements of `foo`. If you want to show content of `bar` directory you need to use `getSelectedFile`. – Pshemo Sep 20 '15 at 19:02
  • Also what do you mean by "all sub directories"? Do you also include nested directories? If I select `\foo` should It return `\foo\x`, `\foo\y` or also `\foo\x\1`? – Pshemo Sep 20 '15 at 19:06
  • yes, i mean if I select \foo i should get everything in \foo plus everything in \foo\x, \foo\x, \foo\x\1. – javanewb Sep 20 '15 at 19:19
  • http://stackoverflow.com/questions/14676407/list-all-files-in-the-folder-and-also-sub-folders – Pshemo Sep 20 '15 at 20:19

0 Answers0