图片
package com.lpc.utils;import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;public class ImageFinder {public static void main(String[] args) {String destinationFolder = "D:/images"; // 您希望存储图片的目标文件夹List<String> imageExtensions = new ArrayList<>();imageExtensions.add(".jpg");imageExtensions.add(".jpeg");imageExtensions.add(".png");imageExtensions.add(".gif");imageExtensions.add(".webp");// 遍历各个磁盘File[] roots = File.listRoots();for (File root : roots) {String rootPath = root.getAbsolutePath();System.out.println("Scanning " + rootPath + "...");scanFolder(rootPath, destinationFolder, imageExtensions);}System.out.println("Image search completed!");}private static void scanFolder(String folderPath, String destinationFolder, List<String> imageExtensions) {File folder = new File(folderPath);File[] files = folder.listFiles();if (files == null) {return;}for (File file : files) {if (file.isDirectory()) {scanFolder(file.getAbsolutePath(), destinationFolder, imageExtensions);} else {String fileName = file.getName();String fileExtension = "";int dotIndex = fileName.lastIndexOf('.');if (dotIndex != -1) {fileExtension = fileName.substring(dotIndex).toLowerCase();} else {continue; // 如果没有扩展名,则跳过当前文件} if (imageExtensions.contains(fileExtension)) {try {// 检查目标文件夹是否存在,若不存在则创建File destinationDir = new File(destinationFolder);if (!destinationDir.exists()) {destinationDir.mkdir();}// 拷贝图片文件到目标文件夹String destinationFilePath = destinationFolder + "/" + fileName;Files.copy(file.toPath(), new File(destinationFilePath).toPath(), StandardCopyOption.REPLACE_EXISTING);// 删除原始图片文件file.delete();System.out.println("Copied " + fileName + " to " + destinationFilePath + " and deleted the original file."); } catch (IOException e) {e.printStackTrace();}}}}}
}
文档
package com.lpc.utils;import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;public class ImageFinder {public static void main(String[] args) {// String destinationFolder = "D:/images"; // 您希望存储图片的目标文件夹String destinationFolder = "D:/docanddocx"; // 您希望存储doc和docx的目标文件夹List<String> imageExtensions = new ArrayList<>();
// imageExtensions.add(".jpg");
// imageExtensions.add(".jpeg");
// imageExtensions.add(".png");
// imageExtensions.add(".gif");
// imageExtensions.add(".webp");imageExtensions.add(".doc");imageExtensions.add(".docx");// 遍历各个磁盘File[] roots = File.listRoots();for (File root : roots) {String rootPath = root.getAbsolutePath();System.out.println("Scanning " + rootPath + "...");scanFolder(rootPath, destinationFolder, imageExtensions);}System.out.println("Image search completed!");}private static void scanFolder(String folderPath, String destinationFolder, List<String> imageExtensions) {File folder = new File(folderPath);File[] files = folder.listFiles();if (files == null) {return;}for (File file : files) {if (file.isDirectory()) {scanFolder(file.getAbsolutePath(), destinationFolder, imageExtensions);} else {String fileName = file.getName();String fileExtension = "";int dotIndex = fileName.lastIndexOf('.');if (dotIndex != -1) {fileExtension = fileName.substring(dotIndex).toLowerCase();} else {continue; // 如果没有扩展名,则跳过当前文件} if (imageExtensions.contains(fileExtension)) {try {// 检查目标文件夹是否存在,若不存在则创建File destinationDir = new File(destinationFolder);if (!destinationDir.exists()) {destinationDir.mkdir();}// 拷贝图片文件到目标文件夹String destinationFilePath = destinationFolder + "/" + fileName;Files.copy(file.toPath(), new File(destinationFilePath).toPath(), StandardCopyOption.REPLACE_EXISTING);// 删除原始图片文件file.delete();System.out.println("Copied " + fileName + " to " + destinationFilePath + " and deleted the original file."); } catch (IOException e) {e.printStackTrace();}}}}}
}