Java文件操作工具类:
import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; @Slf4j public class FileOperationUtils { /** * 检查文件是否存在 * * @param filePath */ public static boolean checkFile(String filePath) { if (StringUtils.isBlank(filePath)) { log.warn("指定目录是空目录:{}", filePath); return false; } File file = new File(filePath); if (file.exists() && file.isFile()) { return true; } else { return false; } } /** * 创建目录 * * @param dirPath */ public static void createDirectory(String dirPath) { File file = new File(dirPath); if (!file.exists()) { file.mkdirs(); } } /** * 删除目录 * * @param dirPath * @return */ public static boolean deleteDirectory(String dirPath) { Path path = Paths.get(dirPath); try { if (Files.exists(path)) { Files.walk(path).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); } return true; } catch (Exception e) { log.error("删除目录失败:", e); } return false; } /** * 删除单个文件 * * @param filePath * @return */ public static boolean deleteFile(String filePath) { if (checkFile(filePath)) { Path path = Paths.get(filePath); try { Files.delete(path); return true; } catch (Exception e) { log.error("删除文件失败:", e); } } return false; } /** * 每次都写新的文件,不追加数据 * * @param filePath * @param dataList * @return */ public static boolean writeFile(String filePath, List<String> dataList) { FileOutputStream fos = null; OutputStreamWriter osw = null; BufferedWriter bw = null; try { fos = new FileOutputStream(filePath, false); osw = new OutputStreamWriter(fos, "UTF-8"); bw = new BufferedWriter(osw); for (String line : dataList) { bw.write(line); bw.newLine(); } bw.flush(); return true; } catch (Exception e) { log.error("写数据到文件失败:", e); } finally { if (bw != null) { try { bw.close(); } catch (IOException e) { } } if (osw != null) { try { osw.close(); } catch (IOException e) { } } if (fos != null) { try { fos.close(); } catch (IOException e) { } } } return false; } /** * 读取文本文件数据 * * @param filePath * @return */ public static List<String> readFile(String filePath) { FileInputStream fis = null; InputStreamReader isr = null; BufferedReader br = null; try { List<String> dataList = new ArrayList<>(); fis = new FileInputStream(filePath); isr = new InputStreamReader(fis, "UTF-8"); br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { line = line.trim(); dataList.add(line); } return dataList; } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { } } if (isr != null) { try { isr.close(); } catch (IOException e) { } } if (fis != null) { try { fis.close(); } catch (IOException e) { } } } return Collections.EMPTY_LIST; } /** * 获取当前目录下所有文件 * @param directoryPath * @return */ public static List<File> getListAllFiles(String directoryPath) { List<File> fileList = new ArrayList<>(); File directory = new File(directoryPath); if (directory.exists() && directory.isDirectory()) { File[] allEntries = directory.listFiles(); if (allEntries != null) { for (File entry : allEntries) { if (entry.isFile()) { fileList.add(entry); } } } } else { log.warn("指定路径无效:{}", directoryPath); } return fileList; } }
朝菌不知晦朔,蟪蛄不知春秋