作为练习,我创建了一个Books列表作为LinkedList,并使用Comparator接口按作者或标题对它们进行排序。 首先,我创建了一类书籍,并确保它将按照我想要的方式进行打印:
class Book {
String title;
String author;
public Book(String t, String a){
title = t;
author = a;
}
public String toString(){
return title + "\t" + author;
}
}
接下来,我创建了一个使用Object Book的LinkedList:
LinkedList<Book> bookList = new LinkedList<>();
创建一个实现Comparator的类,根据title / author进行排序,并将它们显示在主框架内的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";
}
}
}
答案 0 :(得分:4)
按照这样的方式制作您的图书课程Serializable
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;
}
}
序列化接口没有方法或字段,只用于识别可序列化的语义。所以你不需要实现任何方法,只需声明就足够了。
根据java docs
实现java.io.Serializable接口的类启用了类的可序列化。未实现此接口的类将不会将其任何状态序列化或反序列化。可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,仅用于标识可序列化的语义。
更新以解决当前问题
你已经错误地实现了反序列化过程。这是正确的实现。试试这个你会得到理想的结果。
class LoadButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
ObjectInputStream oi = new ObjectInputStream(new FileInputStream("save.ser"));
Object booksIn = oi.readObject();
bookList = (LinkedList<Book>)booksIn;
sortAndDisplay();
oi.close();
} catch (Exception exc){}
}
}