0

Is java able to access directories and their contents located on the computer it's run from? I know that there is a JFileChooser (Where the user can select files), and I know that you can store/open files with direct paths. But can java get all files and directories conntained with a folder?

Can java list file contents of directories stored on a hard drive?

AlanFoster
  • 8,156
  • 5
  • 35
  • 52
  • Yes, it can. So long as the account used to execute the application has the right permissions. – Oded Apr 22 '11 at 20:23

4 Answers4

3

You can get the files under any directory like this:

File[] files = new File("/some/path").listFiles();

You might also be interested in how to recursively list files in Java.

Community
  • 1
  • 1
WhiteFang34
  • 70,765
  • 18
  • 106
  • 111
2

Yes, it can.

So long as the account used to execute the application has the right permissions.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

Yes, it can. In addition to what Oded said, in the case of applets, you need to explicitly grant access to read and write to a hard drive.

Coding District
  • 11,901
  • 4
  • 26
  • 30
0

You can use a filepath to a directory you want and you can do something like:

File directory = new File("PATH TO DIRECTORY");
File [] contents;
if (directory.isDirectory()) {
    contents = directory.listFiles();  // returns an array containing the file and directory abstract paths.
}
MBU
  • 4,998
  • 12
  • 59
  • 98