模拟实时写入文件:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* 将日志信息记录到日志文件
*/
public class LogSvr {
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 将信息记录到日志文件
*
* @param logFile 日志文件
* @param mesInfo 信息
* @throws IOException
*/
public void logMsg(File logFile, String mesInfo) throws IOException {
if (logFile == null) {
throw new IllegalStateException("文件不能为空!");
}
Writer fileWriter = new FileWriter(logFile, true);
fileWriter.write(dateFormat.format(new Date()) + "\t" + mesInfo + "\n");