public void writeLog(String logFileName, String content) {
try {
/* 日志文件大小最大为100KB */
File log = new File(logFileName);
if (log.length() > 102400) {
if (log.delete())
logger.info("日志文件: " + logFileName + " 已满, 已经自动清空.");
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss ");
RandomAccessFile raf = new RandomAccessFile(logFileName, "rw");
String contents = sdf.format(new Date()) + content + "/r/n";
raf.seek(raf.length());
raf.write(contents.getBytes("GBK"));
raf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
调用这个函数就行了,