1

I have this exception I do not understand why it would be thrown or how should I handle it.

        try{
            File file = chooser.getSelectedFile();
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(frame);
            oos.writeObject(list);
            oos.close();
        }catch (IOException e){
            JOptionPane.showMessageDialog(null, e);
        }

I believe the exception is being thrown when oos.writeObject(frame) is read. Here I am attempting to save the JFrame object to binary. Previously I was able to write this same object to XML similarly with an XMLEncoder.

 import java.awt.*;
 import java.awt.event.*;
 import java.beans.*;
 import java.io.*;
 import java.util.ArrayList;

 import javax.swing.*;


 @SuppressWarnings("serial")
 public class SaveFrame implements Serializable{
public static JFileChooser chooser;
public JFrame frame;
public JTextArea textArea;
public ArrayList<String> list = new ArrayList<String>();
public static void main(String[] args){
    chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));      
    SaveFrame test = new SaveFrame();
    test.initialize();
}

public void initialize(){

    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(800, 300);
    frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

    JButton loadXML = new JButton("LoadXML");
    frame.getContentPane().add(loadXML);
    loadXML.addActionListener(EventHandler.create(ActionListener.class, this, "loadXML"));

    JButton saveXML = new JButton("SaveXML");
    frame.getContentPane().add(saveXML);
    saveXML.addActionListener(EventHandler.create(ActionListener.class, this, "saveXML"));

    JButton loadBinary = new JButton("LoadBinary");
    frame.getContentPane().add(loadBinary);
    loadBinary.addActionListener(EventHandler.create(ActionListener.class, this, "loadBinary"));

    JButton saveBinary = new JButton("SaveBinary");
    frame.getContentPane().add(saveBinary);
    saveBinary.addActionListener(EventHandler.create(ActionListener.class, this, "saveBinary"));

    JButton storeData = new JButton("StoreData");
    frame.getContentPane().add(storeData);
    storeData.addActionListener(EventHandler.create(ActionListener.class, this, "storeData"));

    JButton printData = new JButton("PrintData");
    frame.getContentPane().add(printData);

    textArea = new JTextArea(30,30);
    frame.getContentPane().add(textArea);
    printData.addActionListener(EventHandler.create(ActionListener.class, this, "printData"));


    frame.setVisible(true);
}

public void loadXML(){
    int r = chooser.showOpenDialog(null);

    if(r == JFileChooser.APPROVE_OPTION){
        try{
            File file = chooser.getSelectedFile();
            XMLDecoder decoder = new XMLDecoder(new FileInputStream(file));
            decoder.readObject();
            decoder.close();
        }
        catch (IOException e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
}

public void saveXML(){
    if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){
        try{
            File file = chooser.getSelectedFile();
            XMLEncoder encoder = new XMLEncoder(new FileOutputStream(file));
            encoder.writeObject(frame);
            encoder.writeObject(list);
            encoder.close();
        }
        catch (IOException e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
}
public void storeData(){
    list.add(textArea.getText());
}
public void printData(){
    for(String s : list){
        textArea.append(s + " ");
    }
}
public void loadBinary() throws ClassNotFoundException{
    int r = chooser.showOpenDialog(null);

    if(r == JFileChooser.APPROVE_OPTION){
        try{
            File file = chooser.getSelectedFile();
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
            ois.readObject();
            ois.close();
        }
        catch (IOException e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
}
public void saveBinary(){
    if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){
        try{
            File file = chooser.getSelectedFile();
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(frame);
            oos.writeObject(list);
            oos.close();
        }
        catch (IOException e){
            JOptionPane.showMessageDialog(null, e);
             }
          }
      } 
 }

I don't quite get why it works when I'm saving to XML but not to a binary file format. Everything I've read from the documentation of ObjectOutputStream and it's examples would lead me to believe that this should work as XMLEncoder does. I would print out my stack for you but this being a gui, I don't know how to enable that in the console.

4

2 回答 2

0

根据您指出的文档,

只有支持 java.io.Serializable 接口的对象才能写入流。

并且根据java.bean.EventHandler 的Java Doc,它没有实现Serializable

于 2015-08-09T03:01:12.240 回答
0

我不知道您可能已经阅读了哪些内容会导致您对此有所期待。

您链接到的ObjectOutputStreamJavadoc 特别说明了

只有支持java.io.Serializable接口的对象才能写入流。

它需要传递给它的对象以及通过非静态非瞬态引用从它可访问的所有对象来实现Serializable

XMLEncoder没有那个限制。

于 2015-08-09T03:02:18.317 回答