序列化LinkedList<Object>的java语法
作为练习,我将创建一个图书列表作为链接列表,并使用Comparator接口按作者或标题对其进行排序。 首先,我创建了一个图书类,并确保它将以我希望的方式打印到屏幕上:
class Book {
String title;
String author;
public Book(String t, String a){
title = t;
author = a;
}
public String toString(){
return title + "\t" + author;
}
}
接下来,我创建了一个LinkedList,它接受Object Book:
LinkedList<Book> bookList = new LinkedList<>();
创建了一个实现Comparator的类,该类根据标题/作者进行排序,并将它们显示在主框架内的JTextArea上。所有这些都很好,但有一个明显的错误。。。我无法保存文件
我尝试了一个实现Serializable的类,该类将LinkedList作为参数,然后编写一个。obj文件。当我加载它时,它失败了。它创建了文件,但我通常会得到一个NotSerializableException。我还尝试将文件另存为。ser,因为我被告知这样可以更容易地保存它,但在加载时也失败了
有人知道使用BufferedReader序列化LinkedList的好方法吗?还是有其他办法? 提前感谢您花时间阅读此问题,也感谢您提供的任何建议、意见或答案。谢谢,伙计们
补充: 这是全部代码:
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class Check {
BookCompare bc = new BookCompare();
LinkedList<Book> bookList = new LinkedList<>();
JFrame frame;
JPanel northPanel, centerPanel, southPanel;
JButton addBook, saveBook, loadBook;
JTextField authorField, titleField;
JTextArea displayBook;
public static void main(String[] args) {
new Check().buildGui();
}
private void buildGui() {
frame = new JFrame("Book List");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
northPanel = new JPanel();
centerPanel = new JPanel();
southPanel = new JPanel();
addBook = new JButton("Add Book");
addBook.addActionListener(new AddButton());
saveBook = new JButton("Save List");
saveBook.addActionListener(new SaveButton());
loadBook = new JButton("Load List");
loadBook.addActionListener(new LoadButton());
JLabel authorL = new JLabel("Author:");
authorField = new JTextField(10);
JLabel titleL = new JLabel("Title:");
titleField = new JTextField(10);
displayBook = new JTextArea(20,40);
displayBook.setEditable(false);
JScrollPane scroll = new JScrollPane(displayBook);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
northPanel.add(titleL);
northPanel.add(titleField);
northPanel.add(authorL);
northPanel.add(authorField);
centerPanel.add(scroll);
southPanel.add(addBook);
southPanel.add(saveBook);
southPanel.add(loadBook);
frame.getContentPane().add(BorderLayout.NORTH,northPanel);
frame.getContentPane().add(BorderLayout.CENTER,centerPanel);
frame.getContentPane().add(BorderLayout.SOUTH,southPanel);
frame.setVisible(true);
frame.setSize(500, 500);
frame.setResizable(false);
frame.setLocation(375, 50);
}
class AddButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
addToList();
sortAndDisplay();
readyNext();
}
}
private void addToList() {
String newTitle = titleField.getText();
String newAuthor = authorField.getText();
bookList.add(new Book(newTitle, newAuthor));
}
private void sortAndDisplay() {
displayBook.setText(null);
Collections.sort(bookList,bc);
for(int i = 0; i < bookList.size(); i++){
displayBook.append(bookList.get(i).toString());
}
}
private void readyNext() {
authorField.setText(null);
titleField.setText(null);
titleField.requestFocus();
}
class SaveButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream("save.ser"));
oo.writeObject(bookList);
oo.close();
} catch (IOException ioe){}
}
}
class LoadButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
ObjectInputStream oi = new ObjectInputStream(new FileInputStream("save.ser"));
Object booksIn = oi.readObject();
Book inBook = (Book)booksIn;
bookList.add(inBook);
sortAndDisplay();
oi.close();
} catch (Exception exc){}
}
}
class BookCompare implements Comparator<Book> {
public int compare(Book one, Book two) {
return one.title.compareTo(two.title);
}
}
class Book implements Serializable{
String title;
String author;
public Book(String t, String a) {
title = t;
author = a;
}
public String toString(){
return title + "\t" + author + "\n";
}
}
}
# 1 楼答案
让你的图书类
Serializable
像这样序列化接口没有方法或字段,仅用于标识可序列化的语义。所以你不需要实现任何方法,仅仅声明就足够了
根据java docs
更新以解决当前问题
您错误地实现了反序列化过程。这是正确的实现。试试这个你会得到想要的结果