创建日志工具类
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JournalUtil {
public static void WriteJournal(String str){
try {
PrintStream p=new PrintStream(new FileOutputStream("D:\\JAVA\\a.txt",true));
System.setOut(p);
Date date=new Date();
SimpleDateFormat dates=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String dateString=dates.format(date);
System.out.println(dateString+"\t"+str);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
创建测试类
import java.util.Scanner;
public class LogTest {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
JournalUtil.WriteJournal(str);
}
}