将不限大小数据写入CSV文件中 原理:每次都将数据从尾部插入,不限数据条数和文件大小
private static void writeObjectToFile(String fileName , List> datas) throws Exception { RandomAccessFile randomFile = null; try { // 打开一个随机访问文件流,按读写方式 randomFile = new RandomAccessFile(fileName, "rw"); // 文件长度,字节数 if (randomFile.length() == 0) { randomFile.write(StrUtil.join(",", datas.get(0).keySet()).getBytes(CharsetUtil.CHARSET_GBK)); randomFile.writeBytes("rn"); } // 将写文件指针移到文件尾。 long fileLength = randomFile.length(); randomFile.seek(fileLength); for (Map data : datas) { String line = StrUtil.join(",", data.values()).replace("null", ""); randomFile.write(line.getBytes(CharsetUtil.CHARSET_GBK)); randomFile.writeBytes("rn"); } } catch (Exception e) { e.printStackTrace(); } finally { if (randomFile != null) { try { randomFile.close(); } catch (Exception e) { e.printStackTrace(); } } } }
上一篇 Mybatis学习笔记(持续更新)
下一篇 java.lang.IllegalArgumentException: The given host:port (‘yarn-cluster‘) doesn‘t contain a valid por
版权所有 (c)2021-2022 MSHXW.COM
ICP备案号:晋ICP备2021003244-6号