6

I need to hold in memory all absolute paths of file names under a given directory.

myDirectory.list() - retrieves String[] of file names only (without their absolute paths).

Don't want to use File Object since it consumes more memory.

Last thing - I can use apache collections etc. (but didn't find anything useful for that).

user1025852
  • 2,684
  • 11
  • 36
  • 58

2 Answers2

10
String directory = <your_directory>;
File[] files = new File(directory).listFiles();
for(File file : files){
  if(file.isFile()){
    System.out.println(file.getAbsolutePath());
  }
}

This works, and I gotta say I'm confused when you say you don't wanna use File objects, but whatever works, I guess.

victorantunes
  • 1,141
  • 9
  • 20
  • Thanks appreciate it. Using File object is much more memory expensive then just holding the string of path. Case I'll handle folder with 100K files I'm afraid of out of memory etc. So - I'm looking for a neat solution to get String[] of all absolute files paths within a folder – user1025852 Aug 27 '13 at 04:10
  • 1
    If you already know the root path and relative paths of your files and you're not willing to perform another iteration to concatenate those strings, and you're also not willing to use File objects due to afraid of running out of memory, you should probably break your array into smaller arrays and process them separately following the steps provided. – victorantunes Aug 27 '13 at 11:43
-1

Doesn't myDirectory holds the directory of all those files? If so, just combine the path in myDirectory with each of the cells in the array myDirectory.list() returns.

Sharon Dorot
  • 542
  • 1
  • 4
  • 15