Если у нас String остался неизменным, то в чем смысл такой сериализации, когда мы вроде как должны получить актуальные данные, но получаем старые в строке, а по факту дефолтные. как получить строку с актуальными данными в этой задаче?
public class Solution implements Serializable {
public static void main(String[] args) throws IOException, ClassNotFoundException {
File file = new File("D:\\Study2\\JavaRushTasks\\2.JavaCore\\src\\com\\javarush\\task\\task20\\task2014\\file1");
ObjectOutput oo = new ObjectOutputStream(new FileOutputStream(file));
Solution savedObject = new Solution(5);
System.out.println("saved: " + savedObject); // saved: Today is 16 мая 2020, суббота, and the current temperature is 5 C
oo.writeObject(savedObject);
oo.close();
Solution loadedObject = new Solution(8);
System.out.println("before " + loadedObject); // before Today is 16 мая 2020, суббота, and the current temperature is 8 C
ObjectInputStream oi = new ObjectInputStream(new FileInputStream(file));
loadedObject = (Solution) oi.readObject();
oi.close();
System.out.println("after: " + loadedObject); // after:Today is 16 мая 2020, суббота, and the current temperature is 5 C
System.out.println(loadedObject.temperature); // 0
}
private final transient String pattern = "dd MMMM yyyy, EEEE";
private transient Date currentDate;
private transient int temperature;
String string;
public Solution() {
}
public Solution(int temperature) {
this.currentDate = new Date();
this.temperature = temperature;
string = "Today is %s, and the current temperature is %s C";
SimpleDateFormat format = new SimpleDateFormat(pattern);
this.string = String.format(string, format.format(currentDate), temperature);
}
@Override
public String toString() {
return this.string + ". Temp = " + this.temperature;
}
}