简体   繁体   English

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

[英]Java, saved file in object, can't retrieve it

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) 您可以通过调用personFile.close()将其personFile.close()读取对象后也是如此)

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 : 顺便说一句,您不需要关闭两个流,如close

If this stream has an associated channel then the channel is closed as well. 如果此流具有关联的通道,则该通道也将关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM