سؤال

Hello i am having an issue with serializing an object. what i actually want to do is fetch record from server serialize it using a hashmap and save it in sqllite db.

i can save the object serialized to my memory card but when i deserialize it nohting comes out and all my previous data is wiped out.

Here is my sample code for serialzation

Serialization

Employee e = new Employee();
  e.name = "Reyan Ali";
  e.address = "Phokka Kuan, Ambehta Peer";
  e.SSN = 11122333;
  e.number = 101;
  try
  {
     FileOutputStream fileOut =
     new FileOutputStream("/tmp/employee.ser");
     ObjectOutputStream out = new ObjectOutputStream(fileOut);
     out.writeObject(e);
     out.close();
     fileOut.close();
     System.out.printf("Serialized data is saved in /tmp/employee.ser");
  }catch(IOException i)
  {
      i.printStackTrace();
  }

}

Deserialization

  Employee e = null;
  try
  {
     FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
     ObjectInputStream in = new ObjectInputStream(fileIn);
     e = (Employee) in.readObject();
     in.close();
     fileIn.close();
  }catch(IOException i)
  {
     i.printStackTrace();
     return;
  }catch(ClassNotFoundException c)
  {
     System.out.println("Employee class not found");
     c.printStackTrace();
     return;
  }
  System.out.println("Deserialized Employee...");
  System.out.println("Name: " + e.name);
  System.out.println("Address: " + e.address);
  System.out.println("SSN: " + e.SSN);
  System.out.println("Number: " + e.number);
}

Secondly what would be the type of column in sqlite which can save serialized objects. if anyone of you have a good tutorial please share.

هل كانت مفيدة؟

المحلول

Made some changes in your code as below.

First implement Serializable interface in class Employee.java

public void serailize() {
    Employee e = new Employee();
    e.setName("Reyan Ali");
    e.setAddress("Phokka Kuan, Ambehta Peer");
    e.setSSN(11122333);
    e.setNumber(101);
    try {
        ObjectOutputStream out = new ObjectOutputStream(openFileOutput(
                "employee.ser", MODE_PRIVATE));
        out.writeObject(e);
        out.close();
        System.out.printf("Serialized data is saved in /tmp/employee.ser");
    } catch (IOException i) {
        i.printStackTrace();
    }
}

public void deSerailize() {
    Employee e = null;
    try {
        ObjectInputStream in = new ObjectInputStream(
                openFileInput("employee.ser"));
        e = (Employee) in.readObject();
        in.close();
    } catch (IOException i) {
        i.printStackTrace();
        return;
    } catch (ClassNotFoundException c) {
        System.out.println("Serialized class not found");
        c.printStackTrace();
        return;
    }
    System.out.println("Deserialized Employee...");
    System.out.println("Name: " + e.name);
    System.out.println("Address: " + e.address);
    System.out.println("SSN: " + e.SSN);
    System.out.println("Number: " + e.number);
}

OpenFileInput() and openFileOutPut() are the application private files.

I think it is good to store serialized data in files.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top