Get all file names in directory using JFileChooser?

I'm using this bit of code:

 fileBrowser() {
      String toReturn = null;
      JFileChooser Chooser = new JFileChooser();
      int choosen = Chooser.showOpenDialog(fileBrowser.this);
      if (choosen == JFileChooser.APPROVE_OPTION) {         
            System.out.println(Chooser.getCurrentDirectory().toString()+"\\"+Chooser.getSelectedFile().getName());
      }

  }

To get the selected file name and location, which is all working fine. I was wondering as an addition, is there also a way to get all the filenames in that directory as well? something like .getAllFiles() I've had a search around and can't find one?

Thanks in Advance.

Answers


Sure, use

File[] filesInDirectory = chooser.getCurrentDirectory().listFiles();

Then you can iterate over that array:

for ( File file : filesInDirectory ) {
    System.out.println(file.getName());
}

Need Your Help

Pointers to pointers in C

c unix pointers

I'm trying to understand how pointers to pointers work and I came out with this example, and it compiles fine. But, when it is executed, I get a segmentation fault.

No error for not initializing reference variable of class

c++ oop constructor

I am a newbie and have a basic doubt about relationship between object creation and constructors.