一、需求描述
项目需求:想要制作一个可以根据输入一个指定的文件夹路径,可以获取指定类型的文件路径(本实例是jpg和png图片路径输出的代码),可以扫描到文件夹中子文件夹的图片并输出,同时拥有一个可以去除路径中不需要的前缀的代码案例。
二、解决办法
想必大家在搜索想要的相关案例时,大多数都是用来借鉴修改的(或者直接使用的),所以为了不让大家感到麻烦就先直接贴出一个可以运行代码,想要使用的可以直接使用,只需要按照我的类名建一个类,即可运行,因为我的这个整体代码是把所有方法写在一起了,包括主方法,大家完全可以把我这个代码当工具来用,所以话不多说直接看完整代码,详细讲解看第三部分:
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class ImageFileExporter {
public static void main(String[] args) {
String folderPath = "D:\\1\\image"; // 替换为你的文件夹路径
String prefixToRemove = "D:\\1\\"; // 要移除的前缀
List<String> imagePaths = getImagePaths(folderPath, prefixToRemove);
exportToTxt(imagePaths);
}
private static List<String> getImagePaths(String folderPath, String prefixToRemove) {
List<String> imagePaths = new ArrayList<>();
try {
// 获取一级文件夹列表
List<Path> topLevelFolders = new ArrayList<>();
Files.list(Paths.get(folderPath)).forEach(filePath -> {
if (Files.isDirectory(filePath)) {
topLevelFolders.add(filePath);
} else if (isImageFile(filePath.toString())) {
// 如果当前文件夹直接包含图片文件,添加到列表中
String imagePath = filePath.toString();
if (imagePath.startsWith(prefixToRemove)) {
imagePath = imagePath.substring(prefixToRemove.length());
}
imagePath = imagePath.replace("\\", "/"); // 将斜杠替换为反斜杠
imagePaths.add(imagePath);
}
});
// 对一级文件夹进行处理,确保子文件夹内的内容先输出
for (Path folder : topLevelFolders) {
imagePaths.addAll(getImagePathsInSubfolders(folder, prefixToRemove));
}
} catch (IOException e) {
e.printStackTrace();
}
return imagePaths;
}
private static List<String> getImagePathsInSubfolders(Path folder, String prefixToRemove) {
List<String> imagePaths = new ArrayList<>();
try {
// 遍历子文件夹
Files.list(folder).forEach(filePath -> {
if (Files.isDirectory(filePath)) {
// 递归调用,处理子文件夹内的内容
imagePaths.addAll(getImagePathsInSubfolders(filePath, prefixToRemove));
} else if (isImageFile(filePath.toString())) {
// 如果是图片文件,添加到列表中
String imagePath = filePath.toString();
if (imagePath.startsWith(prefixToRemove)) {
imagePath = imagePath.substring(prefixToRemove.length());
}
imagePath = imagePath.replace("\\", "/"); // 将斜杠替换为反斜杠
imagePaths.add(imagePath);
}
});
} catch (IOException e) {
e.printStackTrace();
}
return imagePaths;
}
private static boolean isImageFile(String fileName) {
String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
return extension.equalsIgnoreCase("jpg") || extension.equalsIgnoreCase("png");
}
private static void exportToTxt(List<String> imagePaths) {
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("image_paths.txt"))) {
// 对所有文件路径按字母顺序排序
imagePaths.sort(Comparator.naturalOrder());
for (String imagePath : imagePaths) {
writer.write(imagePath);
writer.newLine();
}
System.out.println("图片路径已成功导出到 image_paths.txt 文件中。");
} catch (IOException e) {
e.printStackTrace();
}
}
}
三、详细讲解
1.main方法
public static void main(String[] args) {
String folderPath = "D:\\1\\image"; // 替换为你的文件夹路径
String prefixToRemove = "D:\\1\\"; // 要移除的前缀
List<String> imagePaths = getImagePaths(folderPath, prefixToRemove);
exportToTxt(imagePaths);
}
这个 main
方法是程序的入口点。它调用了两个主要的方法:getImagePaths
和 exportToTxt
,并且提供了文件夹路径和要移除的前缀作为参数。
2.getImagePaths方法
private static List<String> getImagePaths(String folderPath, String prefixToRemove) {
List<String> imagePaths = new ArrayList<>();
try {
List<Path> topLevelFolders = new ArrayList<>();
Files.list(Paths.get(folderPath)).forEach(filePath -> {
if (Files.isDirectory(filePath)) {
topLevelFolders.add(filePath);
} else if (isImageFile(filePath.toString())) {
String imagePath = filePath.toString();
if (imagePath.startsWith(prefixToRemove)) {
imagePath = imagePath.substring(prefixToRemove.length());
}
imagePath = imagePath.replace("/", "\\");
imagePaths.add(imagePath);
}
});
for (Path folder : topLevelFolders) {
imagePaths.addAll(getImagePathsInSubfolders(folder, prefixToRemove));
}
} catch (IOException e) {
e.printStackTrace();
}
return imagePaths;
}
这个方法负责获取给定文件夹路径中的所有图片文件的路径。它首先遍历了一级文件夹,如果文件是一个文件夹,就将其添加到一个列表中;如果文件是一个图片文件,就将其路径添加到 imagePaths 列表中。然后,它对一级文件夹进行循环处理,调用 getImagePathsInSubfolders 方法获取子文件夹内的图片路径,并将其添加到 imagePaths 列表中。最后,返回包含所有图片路径的列表。
3. getImagePathsInSubfolders 方法
private static List<String> getImagePathsInSubfolders(Path folder, String prefixToRemove) {
List<String> imagePaths = new ArrayList<>();
try {
Files.list(folder).forEach(filePath -> {
if (Files.isDirectory(filePath)) {
imagePaths.addAll(getImagePathsInSubfolders(filePath, prefixToRemove));
} else if (isImageFile(filePath.toString())) {
String imagePath = filePath.toString();
if (imagePath.startsWith(prefixToRemove)) {
imagePath = imagePath.substring(prefixToRemove.length());
}
imagePath = imagePath.replace("/", "\\");
imagePaths.add(imagePath);
}
});
} catch (IOException e) {
e.printStackTrace();
}
return imagePaths;
}
这个方法与 getImagePaths 方法类似,但是它是用来处理子文件夹内的内容的。它递归地遍历了给定文件夹内的所有子文件夹,并且对每个子文件夹调用自身以获取其中的图片路径。
imagePath = imagePath.replace("\\", "/"); // 将斜杠替换为反斜杠
这句代码是用来将\替换成/的,这个是我自己的使用要求,大家可以按需修改。
4. isImageFile 方法
private static boolean isImageFile(String fileName) {
String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
return extension.equalsIgnoreCase("jpg") || extension.equalsIgnoreCase("png");
}
这个方法用于检查给定文件名是否表示一个图片文件。它提取文件扩展名,并检查是否为 "jpg" 或 "png",其实这块可以做更多的扩展,不一定非得是图片,比如其他文件也可以(你懂的)。
5. exportToTxt 方法
private static void exportToTxt(List<String> imagePaths) {
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("image_paths.txt"))) {
imagePaths.sort(Comparator.naturalOrder());
for (String imagePath : imagePaths) {
writer.write(imagePath);
writer.newLine();
}
System.out.println("图片路径已成功导出到 image_paths.txt 文件中。");
} catch (IOException e) {
e.printStackTrace();
}
}
最后,这个方法负责将图片路径导出到一个文本文件中。它首先对图片路径进行排序,然后将每个路径写入文件中,并在写入完毕后打印一条成功消息。
总结:
其实正常来说这个程序应该把每个方法创建一个类,不过博主急着使用工具,所以也就没在意这些,建议初学者可以自己整理成多个类,然后多个类之间相互调用,这样更符合编程习惯,而且也是一种很好的练习方式,如果大家只是想使用该代码作为工具,可以不用在意这些(大佬可以略过),要是初学者建议还是深入理解一下代码,然后自己改进一下。