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
  • 7,818
  • 5
  • 34
  • 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
  • 68,826
  • 17
  • 104
  • 110
2

Yes, it can.

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

Oded
  • 477,625
  • 97
  • 867
  • 998
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,801
  • 4
  • 24
  • 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,858
  • 12
  • 58
  • 97