gpt4 book ai didi

java - 使用可序列化保存对象不起作用?

转载 作者:行者123 更新时间:2023-12-02 04:54:16 25 4
gpt4 key购买 nike

我正在使用图形 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();
}

}

最佳答案

从phonebook类来看,第一个问题是该类没有实现Serialized接口(interface)。

我想您应该再次翻阅书籍并深入探讨主题:“如何序列化对象”。仅仅打开一个流并将对象扔到该流中是行不通的。

关于java - 使用可序列化保存对象不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28928850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com