Почему в файл выводит только последнее время? - Java
Формулировка задачи:
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.Locale;
import java.text.DateFormat;
public class Time {
public Time(){
Timer timer;
File output = new File("output.txt");
timer = new Timer(1000, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ea) {
String format;
format=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM).format(new Date());
System.out.println(format);
try( FileWriter fw = new FileWriter(output)){
fw.write(format);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("I/O Error: " + e);
}
}});
timer.start();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Locale local = new Locale("us","US");
System.out.println(" Отображение времени:");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Time();
}
});
}
}Решение задачи: «Почему в файл выводит только последнее время?»
textual
Листинг программы
try( FileWriter fw = new FileWriter(output,true)){
String lineSeparator = System.getProperty("line.separator");
fw.write(format + lineSeparator);