This was my first time ever trying to save object in a file, so I have no idea where I’m going wrong. This is just a test program, the original one is much larger. Save is successful, backup file is created. However I can’t seem to recall that file/object. Compiling works though. Could someone please explain where exactly did I go wrong. And in a little bit more ‘beginner tutorial’ style please, I’m really bad with ‘Serializable’
import java.io.*;
import java.util.*;
class save {
public static void main(String[] args) {
HashMap<String, Person> list = new HashMap<String, Person>();
Person person = new Person("12", "AAA", "XXX");
list.put(person.getID(), person);
if( list.containsKey(person.getID()))
System.out.println(list.get(person.getID()));
savePerson(person);
list.remove(person.getID());
if( list.containsKey(person.getID()))
System.out.println(list.get(person.getID()));
else
System.out.println("Person is not available");
person = loadPerson("12");
System.out.println(list.size());
}
protected static void savePerson(Person person) {
File source = new File("person"+person.getID()+".data");
try { source.createNewFile(); } catch(IOException e) {System.out.println("Can't create new file : " +e.getMessage());}
try {
FileOutputStream personFile = new FileOutputStream("person"+person.getID()+".data");
try {
ObjectOutputStream personObj = new ObjectOutputStream (personFile);
personObj.writeObject(person);
personObj.close();
personFile.close();
} catch(IOException e){System.out.println("Can't save the object :" +e.getMessage());}
} catch(FileNotFoundException e){System.out.println("Can't read the damn file :" +e.getMessage());}
}
protected static Person loadPerson(String ID) {
Person person = null;
try {
FileInputStream personFile = new FileInputStream("person"+ID+".data");
try {
ObjectInputStream personObj = new ObjectInputStream(personFile);
try {
person = (Person)personObj.readObject();
personObj.close();
personFile.close();
} catch(ClassNotFoundException e){System.out.println("Can't find the class :" +e.getMessage());}
} catch(IOException e){System.out.println("Can't save the object :" +e.getMessage());}
} catch(FileNotFoundException e){System.out.println("Can't read the damn file :" +e.getMessage());}
return person;
}
}
EDIT: Here’s the person class on the request:
import java.io.*;
class Person implements Serializable {
private String id;
private String fname;
private String lname;
public Person(String id, String fname, String lname) {
this.id = id;
this.fname = fname;
this.lname = lname;
}
protected String getID() { return id; }
protected String getFname() { return fname; }
protected String getLname() { return lname; }
protected void setFname(String newFname) { fname = newFname; }
public String toString() {
return id + ", " + fname + " " + lname;
}
}
Close the file after writing to it. You probably don’t get access to the file. Also, if you will fill the empty catch blocks with print of the exception, you’ll be closer to find the problem.
You can close it by calling
personFile.close()(and the same after reading the object)EDIT: I tested your (new) code, and it works just fine. I was able to read the object, but your code does nothing with it.
BTW, you don’t need to close both stream, as stated in
close: