如何用Java Swing做模版?(十万火急!!!!!!) Document中是不是有不能序列化的对象呀参照http://www.csdn.net/expert/topic/637/637704.xml?temp=.5445368好象是没问题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 下面这个例子就序列化包含日文的对象,没问题的(不好意思,我的系统是日文的)我感觉应该是你的Document的中包含有没有继承serialable接口的对象如果不是的话,你可以把代码贴出来,我帮你看一下import java.io.*;import javax.swing.*;public class test{ JTextField tf = new JTextField("核"); public static void main(String [] arg){ test test1 = new test(); test1.invoke(); } public void invoke(){ try{ ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("1.dat")); os.writeObject(tf); os.flush(); os.close(); System.out.println("Before Serial" + tf.getText()); System.out.println("****************************"); ObjectInputStream in= new ObjectInputStream(new FileInputStream("1.dat")); JTextField tfnew = (JTextField)in.readObject(); in.close(); System.out.println("After Serial" + tfnew.getText()); }catch(Exception e){} }} //读出文件中对象的类:class OpenAction extends AbstractAction { OpenAction() { super(openAction); } public void actionPerformed(ActionEvent e) { Frame frame = getFrame(); if (fileDialog == null) { fileDialog = new FileDialog(frame); } fileDialog.setMode(FileDialog.LOAD); fileDialog.show(); String file = fileDialog.getFile(); if (file == null) { return; } String directory = fileDialog.getDirectory(); File f = new File(directory, file); if (f.exists()) { try { FileInputStream fin = new FileInputStream(f); ObjectInputStream istrm = new ObjectInputStream(fin); Document doc = (Document) istrm.readObject(); if(getEditor().getDocument() != null) getEditor().getDocument().removeUndoableEditListener (undoHandler); getEditor().setDocument(doc); doc.addUndoableEditListener(undoHandler); resetUndoManager(); frame.setTitle(file); validate(); } catch (IOException io) { // should put in status panel System.err.println("IOException: " + io.getMessage()); } catch (ClassNotFoundException cnf) { // should put in status panel System.out.println("Read Errors!"); System.err.println("Class not found: " + cnf.getMessage()); } } else { // should put in status panel System.err.println("No such file: " + f); } } }//将对象写入文件的类:class SaveAction extends AbstractAction { SaveAction() { super(saveAction); } public void actionPerformed(ActionEvent e) { Frame frame = getFrame(); if (fileDialog == null) { fileDialog = new FileDialog(frame); } fileDialog.setMode(FileDialog.SAVE); fileDialog.show(); String file = fileDialog.getFile(); if (file == null) { return; } String directory = fileDialog.getDirectory(); File f = new File(directory, file); try { FileOutputStream fstrm = new FileOutputStream(f); ObjectOutput ostrm = new ObjectOutputStream(fstrm); //ByteArrayOutputStream bastrm = new ByteArrayOutputStream() ostrm.writeObject(getEditor().getDocument()); ostrm.flush(); } catch (IOException io) { // should put in status panel System.out.println("Write Errors!"); System.err.println("IOException: " + io.getMessage()); } } }另:我现在就是在JDK的Sample中的Stylepad.jar中打包的几个Java文件基础上进行修改,这个类也是现成的,不是我写的。所以……惭愧!希望能够指点迷津。 对了,你的那段程序在我中文系统的环境下怎么出错了!java.io.NotSerializableException: java.lang.Object我都搞不懂了,难道是SUN对中文的支持问题?怎么会日文环境可以,而中文不行?!麻烦看看,谢谢! 返回值为什么不能用来区分重载方法 关于list排序问题 对象是在堆里分配空间的 那对象里的函数里的变量是在那里分配空间的呢? 纠错:逻辑出问题了,谁给治治 我做的网站类 String str=new String("abc");到底创建了几个对象?? 有关反编译 jdbc的问题? Properties的小小小问题,请指教!!! 关于java正则表达式的疑惑,忘前辈们赐教 remove "Java Applet Window" ? 我的程序需要一个特定的jar包,我要把这个包放在那里才能使我的程序正常运行呢!
我感觉应该是你的Document的中包含有没有继承serialable接口的对象
如果不是的话,你可以把代码贴出来,我帮你看一下
import java.io.*;
import javax.swing.*;
public class test{
JTextField tf = new JTextField("核");
public static void main(String [] arg){
test test1 = new test();
test1.invoke();
}
public void invoke(){
try{
ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("1.dat"));
os.writeObject(tf);
os.flush();
os.close();
System.out.println("Before Serial" + tf.getText());
System.out.println("****************************");
ObjectInputStream in= new ObjectInputStream(new FileInputStream("1.dat"));
JTextField tfnew = (JTextField)in.readObject();
in.close();
System.out.println("After Serial" + tfnew.getText());
}catch(Exception e){}
}
}
class OpenAction extends AbstractAction { OpenAction() {
super(openAction);
} public void actionPerformed(ActionEvent e) {
Frame frame = getFrame();
if (fileDialog == null) {
fileDialog = new FileDialog(frame);
}
fileDialog.setMode(FileDialog.LOAD);
fileDialog.show(); String file = fileDialog.getFile();
if (file == null) {
return;
}
String directory = fileDialog.getDirectory();
File f = new File(directory, file);
if (f.exists()) {
try {
FileInputStream fin = new FileInputStream(f);
ObjectInputStream istrm = new ObjectInputStream(fin);
Document doc = (Document) istrm.readObject();
if(getEditor().getDocument() != null)
getEditor().getDocument().removeUndoableEditListener
(undoHandler);
getEditor().setDocument(doc);
doc.addUndoableEditListener(undoHandler);
resetUndoManager();
frame.setTitle(file);
validate();
} catch (IOException io) {
// should put in status panel
System.err.println("IOException: " + io.getMessage());
} catch (ClassNotFoundException cnf) {
// should put in status panel
System.out.println("Read Errors!");
System.err.println("Class not found: " + cnf.getMessage());
}
} else {
// should put in status panel
System.err.println("No such file: " + f);
}
}
}//将对象写入文件的类:
class SaveAction extends AbstractAction { SaveAction() {
super(saveAction);
} public void actionPerformed(ActionEvent e) {
Frame frame = getFrame();
if (fileDialog == null) {
fileDialog = new FileDialog(frame);
}
fileDialog.setMode(FileDialog.SAVE);
fileDialog.show();
String file = fileDialog.getFile();
if (file == null) {
return;
}
String directory = fileDialog.getDirectory();
File f = new File(directory, file);
try {
FileOutputStream fstrm = new FileOutputStream(f);
ObjectOutput ostrm = new ObjectOutputStream(fstrm);
//ByteArrayOutputStream bastrm = new ByteArrayOutputStream()
ostrm.writeObject(getEditor().getDocument());
ostrm.flush();
} catch (IOException io) {
// should put in status panel
System.out.println("Write Errors!");
System.err.println("IOException: " + io.getMessage());
}
}
}另:我现在就是在JDK的Sample中的Stylepad.jar中打包的几个Java文件基础上进行修改,这个类也是现成的,不是我写的。所以……
惭愧!希望能够指点迷津。
java.io.NotSerializableException: java.lang.Object
我都搞不懂了,难道是SUN对中文的支持问题?
怎么会日文环境可以,而中文不行?!
麻烦看看,谢谢!