txt写入

/**
*
* @author zhangqiang
* txt写入
* @date 2017-2-4
* @return
*
*/
public static void contentToTxt(String content) {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateNowStr = sdf.format(d);

String str = new String(); //原有txt内容
String s1 = new String();//内容更新
try { //"D:\ZHJKTomcat7\logs\smslogs\sms_"+dateNowStr+"_logs.txt"
//"C:\logs\smslogs\sms_"+dateNowStr+"_logs.txt"
File f = new File("D:\ZHJKTomcat7\logs\smslogs\sms_"+dateNowStr+"_logs.txt");
if (f.exists()) {
//System.out.print("文件存在");
} else {
//System.out.print("文件不存在");
f.createNewFile();// 不存在则创建
}
BufferedReader input = new BufferedReader(new FileReader(f));

while ((str = input.readLine()) != null) {
s1 += str + " ";
}
input.close();
s1 += content;

BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

原文地址:https://www.cnblogs.com/LIFE-bug/p/6479215.html