我尝试使用ObjectOutputStream从SWT打开/保存文本对象。但它不起作用。有什么想法,为什么?
public static void read(String fileName, Text textField) {
int c=0;
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
c= in.readInt();
textField = (Text) in.readObject();
in.close();
}
catch(IOException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void write(String fileName, Text textField) {
int c = 1;
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName));
out.writeInt(c);
out.writeObject((Text)textField);
out.close();
}
catch(IOException e){
e.printStackTrace();
}
}
我保存时控制台上显示的错误:
java.io.NotSerializableException: org.eclipse.swt.widgets.Text
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at FileIO.write(FileIO.java:42)
at SelectionAdapterSave.widgetSelected(SelectionAdapterSave.java:29)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1057)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4170)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3759)
at Editor.open(Editor.java:230)
at EditorMain.main(EditorMain.java:6)
这是当我尝试打开文件时,我之前保存的内容:
Caused by: java.io.NotSerializableException: org.eclipse.swt.widgets.Text
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at FileIO.write(FileIO.java:42)
at SelectionAdapterSave.widgetSelected(SelectionAdapterSave.java:29)
... 7 more
答案 0 :(得分:1)
您只能对实现ObjectOutputStream
的对象使用Serializable
。 SWT Text
类没有实现这一点。
SWT Text
类包含依赖于本机UI代码的各种对象,无法再保存和加载它。