Java,在对象中保存文件,无法检索它
这是我第一次尝试将对象保存在文件中,所以我不知道哪里出了问题。这只是一个测试程序,原始程序要大得多。保存成功,备份文件已创建。但是我似乎记不起那个文件/对象。不过编译工作。有人可以解释一下我到底错在哪里吗?请以更多的“初学者教程”风格,我对“可序列化”
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;
}
}
编辑真的很糟糕:这是请求的人员类:
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;
}
}
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
写入后关闭文件。您可能无权访问该文件。另外,如果您使用异常的打印来填充空的 catch 块,您将更容易找到问题。
您可以通过调用 < 来关闭它code>personFile.close() (读取对象后相同)
编辑:我测试了您的(新)代码,它工作得很好。我能够读取该对象,但您的代码对此没有任何作用。
顺便说一句,您不需要关闭两个流,如
close
中所述: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
: