首先要获得目标文件夹的路径path,再用path.listFiles()读取目标文件夹中的所有文件,把文件放到数组files里。
public static ArrayList getPathFilesName(String filePath) {
File path = new File(filePath);// 获得路径
// File path = new File("/mnt/sdcard/");
File[] files = path.listFiles();// 读取文件
ArrayList<String> filesName = new ArrayList<>();
for (int i = 0; i < files.length; i++) {
String fileName = files[i].getName();
String file = null;
if (fileName.endsWith(".xml")) {
file = fileName.substring(0,
fileName.lastIndexOf(".")).toString();
filesName.add(file);
}
if (file != null) {
//file就是我想要获得的以.xml结尾的文件的文件名了
}
}
return filesName;
}
本文介绍了一种使用Java编程语言从指定文件夹中读取所有XML文件的方法,并将这些文件名存储到ArrayList中。
3384

被折叠的 条评论
为什么被折叠?



