序列化链接列表<Object>的语法



作为练习,我将创建一个图书列表作为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;
    }
}

接下来,我创建了一个LinkedList,它采用ObjectBook:

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";
        }
    }
}

让你的Book类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文档

类的可串行性是由实现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){}
    }
 }

相关内容

  • 没有找到相关文章

最新更新