package writetotextfile; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; public class Write { public static void main(String[] args) throws IOException { String filePath = "E:/writetext8/log.txt"; String file = filePath.substring(0, filePath.lastIndexOf("/")); File logfile = new File(file); if (!logfile.exists()) { logfile.mkdirs(); } File log = new File(filePath); if (!log.exists()) { log.createNewFile(); } String content = "\n\n请求来源ip为:\n127.0.0.1\n请求时间为:\n"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss-SSS").format(new Date())+"\n请求参数为:\n666"; writeByFileWrite(filePath,content); } public static void writeByFileWrite(String _sDestFile, String _sContent) throws IOException { FileWriter fw = null; try { fw = new FileWriter(_sDestFile,true); fw.write(_sContent); } catch (Exception ex) { ex.printStackTrace(); } finally { if (fw != null) { fw.close(); fw = null; } } } }