import org.springframework.stereotype.Component; import java.io.*; @Component public class Write { //传入两个参数,content内容,title文件名 public void writeTxt(String content ,String title){ try { //建立文件路径 File writename = new File("C:\\stock_log"); //判断是否存在 if(!writename.exists()){ //不存在就建立 writename.mkdirs(); } //建立文件路径 File writename1 = new File("C:\\stock_log\\"+title+".txt"); //判断是否存在 if(!writename1.exists()) { //不存在就建立 writename1.createNewFile(); } //建立写入文件方式,true为追加写入,原内容不覆盖 FileWriter fw = new FileWriter(writename1,true); //追加写入 fw.append(content+"\n"); //刷新 fw.flush(); //关闭资源 fw.close(); } catch (Exception e) { e.printStackTrace(); } } }
本文只是学习总结。java