java数据list写入文件
/** * 生成数据文件 * * @param data 数据 * @param fileName 文件名 * @return 数据文件对象 * @throws IOException */ private File generateDataFile(List<List<String>> data, String fileName) throws IOException { File file = new File(getTempFile(fileName)); StringBuilder builder = new StringBuilder(); String rowSeparator = "\n"; String fieldSeparator = "\1"; boolean rowSeparatorToEnd = false; boolean fieldSeparatorToEnd = false; int rowIndex = 0; for (List<String> row : data) { int colIndex = 0; for (String col : row) { builder.append(col); //行尾判断是否添加字段分隔符 if (colIndex == row.size() - 1 && !fieldSeparatorToEnd) { continue; } //添加字段分割符 builder.append(fieldSeparator); colIndex++; } //内容尾判断是否添加换行符 if (rowIndex == data.size() - 1 && !rowSeparatorToEnd) { continue; } //添加换行符 builder.append(rowSeparator); rowIndex++; } FileUtils.writeStringToFile(file, builder.toString(), StandardCharsets.UTF_8); return file; } /** * 获取暂时文件的路径 * * @param fileName 文件名 * @return */ public String getTempFile(String fileName) { String uuid = IdUtils.fastSimpleUUID(); String tmpFileName = uuid + BaseConstant.SYMBOL_SLASH + fileName; File tmpFile = FileUtils.getFile(FileUtils.getTempDirectory(), tmpFileName); tmpFile.getParentFile().mkdirs(); return tmpFile.getAbsolutePath(); }
分类:
java
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [翻译] 为什么 Tracebit 用 C# 开发
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· 刚刚!百度搜索“换脑”引爆AI圈,正式接入DeepSeek R1满血版