🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 组件源码 ``` /** * <b>方法描述:</b> 写文件内容 <br/> * <b>创建者:</b> admin <br/> * <b>创建时间:</b> 2018-06-04 21:56:46 <br/> * * @param filename * 入参|文件名|{@link java.lang.String} * @param object * 入参|写入内容|{@link Object} * @param charset * 入参|文件编码|{@link String} * @param mode * 入参|写入方式 0-覆盖 1-追加|{@link int} * @param newline * 入参|换行标志只对列表区别,1:列表换行写入,0 列表不换行|{@link int} * @param newFile * 入参|文件不存在时是否创建文件|boolean * @return -1 异常<br/> * 0 失败<br/> * 1 成功<br/> */ @Component(label = "写文件内容", style = "判断型", type = "同步组件", version = "1.0.0", deprecated = false, author = "admin", date = "2018-06-04 09:56:46") @InParams(param = { @Param(name = "filename", comment = "文件名", type = java.lang.String.class), @Param(name = "object", comment = "写入内容", type = Object.class), @Param(name = "charset", comment = "文件编码", type = String.class), @Param(name = "mode", comment = "写入方式 0-覆盖 1-追加", type = int.class), @Param(name = "newline", comment = "换行标志只对列表区别,1:列表换行写入,0 列表不换行", type = int.class), @Param(name = "newFile", comment = "文件不存在时是否创建文件", type = boolean.class) }) @Returns(returns = { @Return(id = "-1", desp = "异常"), @Return(id = "0", desp = "失败"), @Return(id = "1", desp = "成功") }) public static ResultBase P_fileWrite(String filename, Object object, String charset, int mode, int newline, boolean newFile) { AppLog.debug("写入文件名:{}", filename); if (filename == null || filename == "" || object == null) { return ResultBase.newFailureResult("TPTF0019", "文件名或写入内容不能为空"); } File s = new File(filename); FileOutputStream fs = null; try { if (!s.exists()) { if (!newFile) { return ResultBase.newFailureResult("TPTF0020", "写文件失败,文件不存在"); } else { s.createNewFile(); } } AppLog.debug("开始准备写入文件:" + filename); fs = new FileOutputStream(filename, (mode != 0)); writeFileBytes(fs, object,charset, newline); fs.flush(); fs.close(); } catch (Exception ex) { AppLog.error(ex); if (fs != null) { try { fs.close(); } catch (Exception e) { AppLog.error(e); } } return ResultBase.newExceptionResult("TPTF0021", "写入文件内容异常:" + AppLog.errorMsg(ex)); } return ResultBase.newSuccessResult(); } ``` # 交易中组件使用方式 ![](https://img.kancloud.cn/e0/89/e089c0858b64c61df7b9d9daec14cffc_1870x890.png) # 参数说明及示例 ## 入口参数 文件名:文件所在的绝对路径,示例: `"D:\\work\\CQYY\\workspace\\abc.txt"` 写入内容:准备写入文件中的内容,示例: `{ "id":"123", "name":"321" }` 文件编码:文件的字符集编码,示例: `"utf-8"` 写入方式 0-覆盖 1-追加:选择写入的方式,示例: `1` 换行标志只对列表区别,1:列表换行写入,0 列表不换行:是否换行,示例: `1` 文件不存在时是否创建文件:是否创建文件,示例: `true`