Java 获取给定文件夹下文件的所有绝对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18444423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get all absolute paths of files under a given folder
提问by user1025852
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).
myDirectory.list() - 仅检索文件名的 String[](没有它们的绝对路径)。
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).
最后一件事 - 我可以使用 apache 集合等(但没有找到任何有用的东西)。
回答by Sharon J D Dorot
Doesn't myDirectoryholds the directory of all those files?
If so, just combine the path in myDirectorywith each of the cells in the array myDirectory.list()returns.
不myDirectory包含所有这些文件的目录吗?如果是这样,只需将路径myDirectory与数组myDirectory.list()返回中的每个单元格组合起来。
回答by victorantunes
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.
这行得通,当你说你不想使用 File 对象时,我不得不说我很困惑,但我想不管怎样都行。

