当前位置: 代码迷 >> java >> 如何在文本文件中写入日志但不覆盖该文件
  详细解决方案

如何在文本文件中写入日志但不覆盖该文件

热度:61   发布时间:2023-07-25 19:13:45.0

如何在文本文件中写日志但不覆盖文件?

Logger logger = Logger.getLogger("MyLog");  
FileHandler fh = new FileHandler("C:/temp/test/MyLogFile.log");  
logger.addHandler(fh);
...
logger.info("log sample");

改变这个:

FileHandler fh = new FileHandler("C:/temp/test/MyLogFile.log");  

对此

FileHandler fh = new FileHandler("C:/temp/test/MyLogFile.log", true);  

如果您阅读文档,您将看到第二个参数指定可选的append参数。 如果为true,您将在(现有)文件的末尾写入,而不会覆盖它。