如何解决JTextPane中序列化的中文问题?

rox 2002-04-30 03:33:03
各位大虾,我现在正好做一个图文混排的东东,主要原理是将图形和文字(及相关样式)输出为二进制流的文件或写入数据库,基本的功能都已实现。
但只要其中的JTextPane里含有中文,再将JTextPane的内容转换为document对象,转成二进制流就会发生问题,输出报错。相关读写操作代码如下:
void openfile_actionPerformed(ActionEvent e) {
Frame frame = getFrame();
if (frame == null) frame = new Frame();
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(textPane.getDocument() != null)
textPane.setDocument(doc);
frame.setTitle(file);
validate();
} catch (IOException io) {
System.err.println("IOException: " + io.getMessage());
} catch (ClassNotFoundException cnf) {
System.err.println("Class not found: " + cnf.getMessage());
}
} else {
System.err.println("No such file: " + f);
}
}//openfile
void savefile_actionPerformed(ActionEvent e) {
Frame frame = getFrame();
if (frame == null) frame = new Frame();
if (fileDialog == null) {
System.out.println("ready");
fileDialog = new FileDialog(frame);
System.out.println("ok");
}
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);
ostrm.writeObject(textPane.getDocument());
ostrm.flush();
} catch (IOException io) {
System.err.println("IOException: " + io.getMessage());
}
}//savefile
可是,写入时报错如下:
IOException: java.lang.Object
读出式报错如下:
IOException: writing aborted; java.io.NotSerializableException: java.lang.Object
此程序在JDK1.3.0环境下,无需重新编译,直接运行,对中文的支持很好,一点问题没有。但JDK1.3.1_02和JDK1.4.0下运行均报错,但公司的工程已经统一使用JDK1.4.0,而且部分主要程序已经是用了新函数,所以要麻烦各位了!
请问,这是不是中文的问题,还是其他的什么原因呢?
如果是中文的问题,是不是要把整个UI或其他的相关的文字属性都设置成为Unicode。那具体该怎么设置呢?
希望各位高手指点小弟的迷津!
不胜感激!
(此程序是JDK的demo中Stylepad.jar源文件的一部分,希望对各位大虾分析问题有所帮助,谢谢!)
...全文
172 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jimjxr 2002-05-01
  • 打赏
  • 举报
回复
bug,见http://developer.java.sun.com/developer/bugParade/bugs/4513615.html,好像没有什么简单的解决方法。
rox 2002-05-01
  • 打赏
  • 举报
回复
谢谢lianyunzxp(小刀):
ostrm.writeObject(textPane);
的方法没有报错。但文件读进来后,就是不正常显示。
我修改后的,代码有错吗?
写文件:
FileOutputStream fstrm = new FileOutputStream(f);
BufferedOutputStream bout = new BufferedOutputStream(fstrm);
ObjectOutputStream ostrm = new ObjectOutputStream(bout);
Object o = (Object)textPane;
ostrm.writeObject(o);
ostrm.flush();
fstrm.flush();
读文件:
FileInputStream fin = new FileInputStream(f);
BufferedInputStream bin = new BufferedInputStream(fin);
ObjectInputStream istrm = new ObjectInputStream(bin);
JTextPane jtp =(JTextPane) istrm.readObject();
if(textPane.getDocument() != null)
textPane = jtp;
validate();
longaway 2002-04-30
  • 打赏
  • 举报
回复
好像有个
getbyte
之类的方法。
longaway 2002-04-30
  • 打赏
  • 举报
回复
手工转码
rox 2002-04-30
  • 打赏
  • 举报
回复
有没有人呀?
rox 2002-04-30
  • 打赏
  • 举报
回复
只有英文的话,一点问题都没有。即使插入图片也没有问题。
如果,使用JDK1.3.0就都没有问题(不需重新编译)
难道是JDK把序列化重写了。
那要怎么做才能真正的支持中文呢?
是不是要对内容编码定义为Unicode,如何做呀!
谢谢!
lianyunzxp 2002-04-30
  • 打赏
  • 举报
回复
看错误的提示好象是你写如的东西不可序列化的错误
难道是Document对象现在不可序列化了吗
你可一这样试试
ostrm.writeObject(textPane);
或者你可以只写如英文,不写中文看看有没有错
lijunyi 2002-04-30
  • 打赏
  • 举报
回复
up
rox 2002-04-30
  • 打赏
  • 举报
回复
document是一个对象,我如何把它转换为byte呢?
最后我还要把它写入文件,我用
FileOutputStream fstrm = new FileOutputStream(f);
ByteArrayOutputStream bout = new ByteArrayOutputStream(fstrm);
ObjectOutputStream ostrm = new ObjectOutputStream(bout);
好像不行。
那如何将ByteArrayOutputStream 写入文件呢?
谢谢!
rox 2002-04-30
  • 打赏
  • 举报
回复
getByte怎么用呀?
那是一个完整的对象,如何转换成byte呀?
我发现ObjectOutputStream和ByteArrayOutputStream都是从OutputStream扩展的,但是我无法在将ByteArrayOutputStream写入到FileInputStream。
我真的很差,不要笑我。我该怎么做呀?

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧