FileUtils
package com.zxwa.ntmss.process.common.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import java.io.*; import java.util.HashSet; import java.util.List; import java.util.Set; public class FileUtil { public static void main(String args[]) { checkRepeat("D://leijirong1.txt"); } public static Set<String> checkRepeat(String fileName) { File file = new File(fileName); BufferedReader reader = null; Set<String> list = new HashSet<String>(); try { System.out.println("以行为单位读取文件内容,一次读一行"); reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次读一行,读入null时文件结束 while ((tempString = reader.readLine()) != null) { if (list.contains(tempString)) { System.out.println(tempString); } else { list.add(tempString); } line++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return list; } public static Set<String> readFileByLines(String fileName) { File file = new File(fileName); BufferedReader reader = null; Set<String> list = new HashSet<String>(); try { System.out.println("以行为单位读取文件内容,一次读一行"); reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次读一行,读入null时文件结束 while ((tempString = reader.readLine()) != null) { list.add(tempString); line++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return list; } public static void writeFileByLines(String fileName, List<String> content) { File file = new File(fileName); FileWriter fw = null; try { if (!file.exists()) { file.createNewFile(); } fw = new FileWriter(file, true); for (String str : content) { fw.append(str).append("\r\n"); } fw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void writeFileByLines(String fileName, String content) { File file = new File(fileName); FileWriter fw = null; try { if (!file.exists()) { file.createNewFile(); } fw = new FileWriter(file, true); fw.append(content).append("\r\n"); fw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void deleteFile(String fileName) { File file = new File(fileName); if (file.exists() && file.isFile()) { file.delete(); } } /** * 将JSON数据格式化并保存到文件中 * * @param t 需要输出的json数 * @param filePath 输出的文件地址 * @return */ public static <T> boolean createJsonFile(T t, String filePath) { String content = JSON.toJSONString(t, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat); boolean flag = true; try { // 保证创建一个新文件 File file = new File(filePath); if (!file.getParentFile().exists()) { // 如果父目录不存在,创建父目录 file.getParentFile().mkdirs(); } if (file.exists()) { // 如果已存在,删除旧文件 file.delete(); } file.createNewFile(); // 将格式化后的字符串写入文件 Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); write.write(content); write.flush(); write.close(); } catch (Exception e) { flag = false; e.printStackTrace(); } return flag; } /** * 读取json 格式的文件 * * @param Path 输出的文件地址 * @return */ public JSONArray ReadFile(String Path) { BufferedReader reader = null; String laststr = ""; try { FileInputStream fileInputStream = new FileInputStream(Path); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8"); reader = new BufferedReader(inputStreamReader); String tempString = null; while ((tempString = reader.readLine()) != null) { laststr += tempString; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return JSONObject.parseArray(laststr); } }
故乡明
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话