1 /* 2 * Copyright (c) 2025 Shenzhen Kaihong Digital. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package utils; 17 18 import java.io.File; 19 import java.io.IOException; 20 import java.io.*; 21 import java.util.ArrayList; 22 import java.util.List; 23 24 /** 25 * <h3>类名:该类用于xxx</h3> 26 * description ${description} 27 * 28 * @author ${USER} 29 * date 2025-02-28 30 * @since 2025-02-28 31 * @version 1.0 32 */ 33 public class FileUtils { 34 35 /** 36 * 创建新文件(若不存在) 37 * 38 * @param filePath 文件路径 39 * @return 是否创建成功 40 */ createFile(String filePath)41 public static boolean createFile(String filePath) { 42 File file = new File(filePath); 43 try { 44 // 文件不存在时创建新文件:ml-citation{ref="2" data="citationList"} 45 return file.createNewFile(); 46 } catch (IOException e) { 47 System.out.println("createFile error: " + e.getMessage()); 48 return false; 49 } 50 } 51 52 /** 53 * 根据文件列表删除文件 54 * 55 * @param files 文件路径 56 */ doDeleteFile(File[] files)57 private static synchronized void doDeleteFile(File[] files) { 58 if (files != null) { 59 for (File child : files) { 60 // 递归调用删除子项:ml-citation{ref="4" data="citationList"} 61 try { 62 deleteFile(child.getCanonicalPath()); 63 } catch (IOException e) { 64 System.out.println("getCanonicalPath error: " + e.getMessage()); 65 } 66 } 67 } 68 } 69 70 /** 71 * 根据路径 72 * 73 * @param path 路径 74 * @return 是否删除成功 75 */ deleteFile(String path)76 public static synchronized boolean deleteFile(String path) { 77 File target = new File(path); 78 if (!target.exists()) { 79 // 路径不存在直接返回失败:ml-citation{ref="3" data="citationList"} 80 return false; 81 } 82 83 try { 84 if (target.isDirectory()) { 85 // 递归删除子文件和空目录:ml-citation{ref="4" data="citationList"} 86 File[] files = target.listFiles(); 87 doDeleteFile(files); 88 } 89 // 删除文件或空目录:ml-citation{ref="3,4" data="citationList"} 90 return target.delete(); 91 } catch (SecurityException e) { 92 System.out.println("deleteFile error: " + e.getMessage()); 93 // 权限不足时返回失败:ml-citation{ref="3" data="citationList"} 94 return false; 95 } 96 } 97 98 /** 99 * 覆盖写入文本内容 100 * 101 * @param filePath 文件路径 102 * @param content 待写入内容 103 */ overwriteText(String filePath, String content)104 public static void overwriteText(String filePath, String content) { 105 try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, false))) { 106 // false 表示覆盖模式:ml-citation{ref="3" data="citationList"} 107 writer.write(content); 108 } catch (IOException e) { 109 System.out.println("overwriteText error: " + e.getMessage()); 110 } 111 } 112 113 /** 114 * 追加文本内容到文件末尾 115 * 116 * @param filePath 文件路径 117 * @param content 待追加内容 118 */ appendText(String filePath, String content)119 public static void appendText(String filePath, String content) { 120 try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) { 121 // true 表示追加模式:ml-citation{ref="6" data="citationList"} 122 writer.write(content); 123 } catch (IOException e) { 124 System.out.println("appendText error: " + e.getMessage()); 125 } 126 } 127 128 /** 129 * 读取文本文件内容 130 * 131 * @param filePath 文件路径 132 * @return 按行读取的内容列表 133 */ readText(String filePath)134 public static List<String> readText(String filePath) { 135 List<String> lines = new ArrayList<>(); 136 try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { 137 // 逐行读取文本:ml-citation{ref="8" data="citationList"} 138 String line; 139 while ((line = reader.readLine()) != null) { 140 lines.add(line); 141 } 142 } catch (IOException e) { 143 System.out.println("readText error: " + e.getMessage()); 144 } 145 return lines; 146 } 147 148 /** 149 * 覆盖写入二进制数据(如图片、音频) 150 * 151 * @param filePath 文件路径 152 * @param data 二进制数据 153 */ overwriteBinary(String filePath, byte[] data)154 public static void overwriteBinary(String filePath, byte[] data) { 155 try (FileOutputStream fos = new FileOutputStream(filePath)) { 156 // 直接覆盖二进制文件:ml-citation{ref="5" data="citationList"} 157 fos.write(data); 158 } catch (IOException e) { 159 System.out.println("overwriteBinary error: " + e.getMessage()); 160 } 161 } 162 163 /** 164 * 读取二进制文件全部内容 165 * 166 * @param filePath 文件路径 167 * @return 字节数组(文件不存在时返回 null) 168 */ readBinary(String filePath)169 public static byte[] readBinary(String filePath) { 170 File file = new File(filePath); 171 // 路径校验:ml-citation{ref="1,3" data="citationList"} 172 if (!file.exists() || file.isDirectory()) { 173 return new byte[0]; 174 } 175 176 try (FileInputStream fis = new FileInputStream(file)) { 177 // 自动关闭流:ml-citation{ref="2,4" data="citationList"} 178 byte[] data = new byte[(int) file.length()]; 179 // 根据文件大小初始化数组 180 // 一次性读取全部内容:ml-citation{ref="2" data="citationList"} 181 int ret = fis.read(data); 182 if (ret > 0) { 183 return data; 184 } 185 return new byte[0]; 186 } catch (IOException e) { 187 System.out.println("readBinary error: " + e.getMessage()); 188 return new byte[0]; 189 } 190 } 191 } 192