使用Serializable保存对象,不起作用?

时间:2015-03-08 16:25:43

标签: java save

我正在使用图形JFrame构建电话簿,并希望将电话簿保存在文件中,以便在我再次启动程序时将其打开。当我按下退出按钮时,我想保存它。但是当我按下Quit时,java会给出:

java.io.NotSerializableException: phonebook.PhoneBook
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at phonebook.QuitButton.actionPerformed(QuitButton.java:23)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3322)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:702)
    at java.awt.EventQueue$3.run(EventQueue.java:696)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:724)
    at java.awt.EventQueue$4.run(EventQueue.java:722)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

以下摘录自我的退出按钮(保存时)

public void actionPerformed(ActionEvent e) {
        try {
            FileOutputStream fout = new FileOutputStream("Sparad Fil");
            ObjectOutputStream out = new ObjectOutputStream(fout);
            out.writeObject(phoneBook);
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(1);
        }
        System.exit(0);
    } 

能够阅读我使用的文件:

public static void main(String[] args) {
        PhoneBook phoneBook = new PhoneBook();
        new PhoneBookGUI(phoneBook);

        try {
            FileInputStream fin = new FileInputStream("Sparad Fil");
            ObjectInputStream in = new ObjectInputStream(fin);
            phoneBook = (PhoneBook) in.readObject();
            in.close();
        } catch (Exception FileNotFoundException) {
            phoneBook = new PhoneBook();
        }
        new PhoneBookGUI(phoneBook);
    }

我的电话簿课程:

public class PhoneBook {
    private Map<String,LinkedList<String>> phoneBook;


    public PhoneBook() {
        phoneBook = new HashMap();
    }

    /** 
     * Associates the specified number with the specified 
     * name in this phone book. 
     * post: If the specified name is not present in this phone book,
     *        the specified name is added and associated  with
     *        the specified number. Otherwise the specified 
     *        number is added to the set of number associated with name.
     * @param name The name for which a phone number is to be added
     * @param number The number associated with the specified name
     * @return true if the specified name and number was inserted
     */
    public boolean put(String name, String number) {
        if(phoneBook.containsKey(name)){
            phoneBook.get(name).add(number);
        }else{
            LinkedList<String> list = new LinkedList<String>();
            list.add(number);
            phoneBook.put(name, list);
        }
        return true;
    }


    /**
     * Removes the the specified name from this phone book.
     * post: If the specified name is present in this phone book,
     *       it is removed. Otherwise this phone book is
     *       unchanged.
     * @param name The name to be removed
     * @return true if the specified name was present
     */
    public boolean remove(String name) {
        if(phoneBook.containsKey(name)){
        phoneBook.remove(name);
        return true;
        }
        return false;
    }

    /**
     * Retrieves a list of phone numbers for the specified name. If the 
     * specified name is not present in this phone book an empty list is 
     * returned.
     * @param name The name whose associated phone numbers are to be returned  
     * @return The phone numbers associated with the specified name
     */
    public List<String> findNumber(String name) {
        LinkedList<String> list = new LinkedList<String>();
        list = phoneBook.get(name);
        return list;
    }

    /**
     * Retrieves a list of names associated with the specified phone number. 
     * If the specified number is not present in this phone book an empty 
     * list is returned.
     * @param number The number for which the set of associated
     * names is to be returned.
     * @return The list of names associated with the specified number
     */
    public List<String> findNames(String number) {
        ArrayList<String> list = new ArrayList<String>();
        for (Map.Entry<String, LinkedList<String>> e : phoneBook.entrySet()) {
            for (String nbr : e.getValue()) {
                if (nbr.equals(number)) {
                    list.add(e.getKey());
                }
            }
        }
        return list;

    }

    /**
     * Retrieves the set of all names present in this phone book.
     * The set's iterator will return the names in ascending order
     * @return The set of all names present in this phone book
     */
    public Set<String> names() {
        return phoneBook.keySet();

    }

    /**
     * Returns true if this phone book is empty
     * @return true if this phone book is empty
     */ 
    public boolean isEmpty() {
        return phoneBook.isEmpty();
    }

    /**
     * Returns the number of names in this phone book
     * @return The number of names in this phone book
     */
    public int size() {
        return phoneBook.size();
    }

}

1 个答案:

答案 0 :(得分:2)

从查看电话簿类开始,第一个问题是该类没有实现Serializable接口。

我想你应该再次转向你的书并深入研究这个主题:“我如何序列化对象”。只是打开一个流并在该流中抛出对象是行不通的。