Java,保存在对象中的文件,无法检索它

时间:2011-11-03 10:04:56

标签: java file object save serializable

这是我第一次尝试将对象保存在文件中,所以我不知道我哪里出错了。这只是一个测试程序,原来的一个要大得多。保存成功,创建备份文件。但是我似乎无法回想起那个文件/对象。虽然编译工作。有人可以解释我到底出错的地方。请更多'初学者教程'风格,我对'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;
    }
}

编辑:这是请求中的人员类:

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;
    }
}

1 个答案:

答案 0 :(得分:3)

写入后关闭文件。您可能无法访问该文件。此外,如果您将使用例外打印填充空的catch块,您将更接近找到问题。

您可以通过调用personFile.close()(读取对象后相同)来关闭它

编辑:我测试了您的(新)代码,它运行正常。我能够读取该对象,但您的代码对此无效。

顺便说一句,您不需要关闭这两个流,如close中所述:

  

如果此流具有关联的频道,则该频道将关闭为   好。