Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get all absolute paths of files under a given folder

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

like image 846
user1025852 Avatar asked Aug 26 '13 12:08

user1025852


People also ask

How do I get the path of all files in a folder?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

Does OS Listdir return full path?

Use os. listdir() function! We'll make a function for this, which simply gets the full path, and returns a list of all such names. Indeed, this gives us the absolute path, from the root directory!

What is the absolute path of a folder?

An absolute path is defined as the specifying the location of a file or directory from the root directory(/). In other words we can say absolute path is a complete path from start of actual filesystem from / directory.

What is absolute path in CMD?

An absolute path contains the full set of directories from the root of the file system up to your target file or directory. On Windows, an absolute path starts with a drive like C:\ . You can cd to an absolute path from anywhere on the filesystem. This is an example absolute path: C:\Users\jesstess\projects.


1 Answers

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.

like image 137
victorantunes Avatar answered Sep 30 '22 15:09

victorantunes