【记录】java 写入内容到文件 自定义文件类型


import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;

public
static boolean writeData2File(String exportPath,String content, String fileName) { boolean flag = false; BufferedWriter out = null; try { if (!ObjectUtils.isEmpty(content) && StringUtils.isNotEmpty(fileName)) { fileName = fileName + ".java"; File pathFile = new File(exportPath); if (!pathFile.exists()) { pathFile.mkdirs(); } String relFilePath = exportPath + File.separator + fileName; File file = new File(relFilePath); if (!file.exists()) { file.createNewFile(); } out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8")); out.write(content); out.newLine(); flag = true; } } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { try { out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } return flag; } }
原文地址:https://www.cnblogs.com/wbl001/p/13334688.html