public class logger {
public static void log(String str) {
PrintStream out=null;
try {
out=new PrintStream(new FileOutputStream("logtext"));
System.setOut(out);
Date nowTime=new Date();
System.out.println(nowTime);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String Time=sdf.format(nowTime);
System.out.println(Time+":"+str);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
if (out != null) {
out.close();
}
}
}
}
public class loggerTest {
public static void main(String[] args) {
logger.log("调用了System类的gc()方法,建议启动垃圾回收");
}
}