Java – get all absolute paths of files under a given folder

absolute-pathapache-commonsjavajava-7list

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).

Best Answer

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.