是否可以持久存储样式文本? [英] Is it possible to store styled text persistently?

查看:20
本文介绍了是否可以持久存储样式文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图序列化一些 DefaultStyledDocument 使用 XMLEncoder 的对象.它们编码得很好,但是当我查看数据时,它不编码任何实际数据,它只提供类文件.我查看了互联网,看到很多人都遇到了这个问题,但没有有用的解决方案.我看到的最佳答案是DefaultStyledDocument 不是一个合适的 bean,所以它不起作用."

So I was trying to serialize some DefaultStyledDocument objects using XMLEncoder. They encode just fine, however when I look at the data, it doesn't encode any actually data, it just gives the class file. I've looked on the internet and saw that many people had trouble with this, but there were no helpful solutions. The best answer I saw was "DefaultStyledDocument isn't a proper bean, so it won't work."

那么,无论如何我可以序列化 DefaultStyledDocuments,而不必处理版本之间的问题?二进制和文本都可以接受.

So, is there anyway I can serialize DefaultStyledDocuments, without having to deal with issues between versions? Both binary and text would be acceptable.

这是我想要做的一些示例代码:

Here's some example code of what I want to do:

DefaultStyledDocument content = new DefaultStyledDocument();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
XMLEncoder encoder = new XMLEncoder(stream);
encoder.writeObject(content);
encoder.close();
stream.toString(); //This is the result of the encoding, which should be able to be decoded to result in the original DefaultStyledDocument

我并不在乎我是否使用 XMLEncoder 或其他一些方法,它只需要工作即可.

I don't really care if I use XMLEncoder or some other method, it just needs to work.

推荐答案

无需使用 XMLEncoder 对文档进行编码.EditorKits 是 Swing Text API 的一部分,可以为您完成这项工作.基础类 EditorKit 具有 read()write() 方法.然后这些方法被各种子编辑器套件扩展以允许读取和写入文档.大多数文档都有自己的 EditorKits,允许程序员读取或写入文档.

There's no need to encode Documents using XMLEncoder. EditorKits, part of the Swing Text API, do that job for you. The base level class, EditorKit, has both a read() and write() method. These methods then get extended by various sub-EditorKits to allow the reading and writing of documents. Most documents have their own EditorKits which allows the programmer to read or write the Document.

然而,StyledEditorKit(DefaultStyledDocument 的自己的"EditorKit)并不容易允许读取或写入.您需要使用支持读写的 RTFEditorKit.但是,Swing 的内置 RTFEditorKit 不能很好地工作.所以有人设计了一个免费的高级"编辑器工具包,这里.为了使用 AdvancedRTFEditorKit 编写 DefaultStyledDocument,请使用以下代码(变量 content 是 DefaultStyledDocument).

However, StyledEditorKit (DefaultStyledDocument's "own" EditorKit) doesn't readily allow reading or writing. You need to use RTFEditorKit, which does support reading and writing. However, Swing's builtin RTFEditorKit does not work very well. So someone designed a free "Advanced" Editor kit available here. In order to write a DefaultStyledDocument with AdvancedRTFEditorKit, using the following code (the variable content is a DefaultStyledDocument).

AdvancedRTFEditorKit editor = new AdvancedRTFEditorKit();
Writer writer = new StringWriter();
editor.write(writer, content, 0, content.getLength());
writer.close();
String RTFText = writer.toString();

可以使用类似的过程通过 RTFEditorKit 的 read() 方法读取 RTFDocument.

A similar process can be used to read RTFDocuments with RTFEditorKit's read() method.

这篇关于是否可以持久存储样式文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆