[英]Android: How can I retain the state of an object on back button Action bar and back button of device
[英]I am serializing an object in Java but upon deserialization, its state is not as expected. How can I retain the object's state?
我正在嘗試在 Java 中構建一個涉及序列化和反序列化的程序。 有兩個類,Employee 和 LauncherClass。
員工.java:
import java.io.Serializable;
public class Employee implements Serializable {
String name;
int id;
int age;
double salary;
public Employee() {
this.name="";
this.id=0;
this.age=0;
this.salary=0.0;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setId(int id)
{
this.id = id;
}
public int getId()
{
return id;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return age;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getSalary()
{
return salary;
}
public String toString()
{
return id + " " + name + " " + age + " " + salary;
}
}
LauncherClass.java:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;
public class LauncherClass {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Scanner sc = new Scanner(System.in);
Employee employee = new Employee();
FileOutputStream fileOutputStream = new FileOutputStream("datafile");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
int choice;
do {
System.out.println("Main Menu");
System.out.println("1.Add an Employee");
System.out.println("2.Display All");
System.out.println("3.Exit");
choice = sc.nextInt();
switch(choice)
{
case 1:
System.out.print("Enter Employee ID:");
employee.setId(sc.nextInt());
sc.nextLine();
System.out.print("Enter Employee Name:");
employee.setName(sc.nextLine());
System.out.print("Enter Employee Age:");
employee.setAge(sc.nextInt());
System.out.print("Enter Employee Salary:");
employee.setSalary(sc.nextDouble());
objectOutputStream.writeObject(employee);
break;
case 2:
System.out.println("----Report----");
FileInputStream fileInputStream = new FileInputStream("datafile");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
while(fileInputStream.available()>0)
{
employee = (Employee)objectInputStream.readObject();
System.out.println(employee);
}
System.out.println("----End of Report---");
objectInputStream.close();
break;
case 3:
System.exit(5);
}
} while (choice!=3);
objectOutputStream.flush();
objectOutputStream.close();
}
}
我執行這個程序並添加一些員工,然后顯示 object 的數據。 到目前為止,一切正常。 但是當我退出並重新啟動程序和 select 顯示選項時,它什么也不顯示。 為什么會這樣? 為什么object中的數據會丟失?
這有很多問題。
您正在打開一個 ObjectOutputStream,在關閉之前您正在打開一個 ObjectInputStream。 你需要解耦你的閱讀和寫作。
由於 System.exit,您的 while 循環將永遠不會讀取 Closeable 關閉/刷新調用。
您需要在使用nextInt()
后使用 System.out.println 因為Scanner.nextInt()
不使用換行符。
您需要將 Closeable 對象包裝在 try-with-resources 中。
另一個用戶是正確的,你需要append,不要覆蓋。
您需要在寫入后調用 ObjectOutputStream#flush 。
public static void main(String[] args) throws IOException, ClassNotFoundException {
Scanner sc = new Scanner(System.in);
Employee employee = new Employee();
int choice;
do {
System.out.println("Main Menu");
System.out.println("1.Add an Employee");
System.out.println("2.Display All");
System.out.println("3.Exit");
choice = sc.nextInt();
switch (choice) {
case 1:
try (FileOutputStream fileOutputStream = new FileOutputStream("datafile", true);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
System.out.println("Enter Employee ID:");
employee.setId(sc.nextInt());
sc.nextLine();
System.out.println("Enter Employee Name:");
employee.setName(sc.nextLine());
System.out.println("Enter Employee Age:");
employee.setAge(sc.nextInt());
System.out.println("Enter Employee Salary:");
employee.setSalary(sc.nextDouble());
objectOutputStream.writeObject(employee);
objectOutputStream.flush();
}
break;
case 2:
System.out.println("----Report----");
try (FileInputStream fileInputStream = new FileInputStream("datafile");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) {
System.out.println("as: " + objectInputStream.available());
while (fileInputStream.available() > 0) {
employee = (Employee) objectInputStream.readObject();
System.out.println(employee);
}
System.out.println("----End of Report---");
}
break;
case 3:
break;
}
} while (choice != 3);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.