我是编程新手并尝试创建日志文件。我编写的代码用于将消息和时间发送到文本文件。但每次我发送另一条消息时,它只会记录上一条消息。我想要一份消息和时间列表,而不是最新的。
public void run () {
String message;
try {
while ((message = reader.readLine()) != null){
System.out.println( "You voted " + message + " You the best") ;
BufferedWriter out = new BufferedWriter(new FileWriter("test.txt"));
out.append(message + "\n");
TimeZone tz = TimeZone.getTimeZone("UTC"); // or PST, MID, etc ...
Date now = new Date();
DateFormat df = new SimpleDateFormat (" yyyy.MM.dd hh:mm:ss ");
df.setTimeZone(tz);
String currentTime = df.format(now);
String timeStamp = new SimpleDateFormat().format( new Date() );
FileWriter aWriter = new FileWriter("test.txt", true);
aWriter.write(currentTime + " " + "\n");
aWriter.flush();
aWriter.close();
out.write(message);
out.close();
everyoneMessage(message);
} // close while
}catch(Exception ex) {ex .printStackTrace();}
} // close run
}// close inner class