- 根据路径获取文件名
public static List<String> getAllFileName(String path) {
File file = new File(path);
File[] tempList = file.listFiles();
List<String> list = new ArrayList<String>();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
System.out.println(tempList[i].getName());
list.add(tempList[i].getName());
}
if (tempList[i].isDirectory()) {
list.addAll(getAllFileName(tempList[i].getAbsolutePath()));
}
}
return list;
}
运行结果
2、根据路径获取文件的绝对路径
public static List<String> getAllFileNameAbsolutePath(String path) {
File file = new File(path);
File[] tempList = file.listFiles();
List<String> list = new ArrayList<String>();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
System.out.println(tempList[i].getAbsolutePath());
list.add(tempList[i].getAbsolutePath());
}
if (tempList[i].isDirectory()) {
list.addAll(getAllFileNameAbsolutePath(tempList[i].getAbsolutePath()));
}
}
return list;
}
运行结果