Java中存取Rtf文件
来源:互联网 发布:json语法 编辑:程序博客网 时间:2024/06/05 03:58
狂找了几天在Java中存取Rtf文件的方法,结果只能用两个字形容——郁闷!!!
本来不想写总结的,不过想 到这几天的辛苦,还是决定把找到的一点小东东写下来,希望能对以后的应用有所启迪。
下面是我找到的唯一可用的一段代码(使用Java中自带的Rtf包):
import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.rtf.*;
class RTFView
extends JFrame
{
public RTFView()
{
setTitle( "RTF Text Application" );
setSize( 400, 240 );
setBackground( Color.gray );
getContentPane().setLayout( new BorderLayout() );
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel, BorderLayout.CENTER );
// Create an RTF editor window
RTFEditorKit rtf = new RTFEditorKit();
JEditorPane editor = new JEditorPane();
editor.setEditorKit( rtf );
editor.setBackground( Color.white );
// This text could be big so add a scroll pane
JScrollPane scroller = new JScrollPane();
scroller.getViewport().add( editor );
topPanel.add( scroller, BorderLayout.CENTER );
// Load an RTF file into the editor
try {
FileInputStream fi = new FileInputStream( "test.rtf" );
rtf.read( fi, editor.getDocument(), 0 );
}
catch( FileNotFoundException e )
{
System.out.println( "File not found" );
}
catch( IOException e )
{
System.out.println( "I/O error" );
}
catch( BadLocationException e )
{
}
}
public static void main( String args[] )
{
// Create an instance of the test application
RTFView mainFrame = new RTFView();
mainFrame.setVisible( true );
}
}
接下来是我自己的测试代码:
RTFEditorKit rtfeditor=new RTFEditorKit();
// jtp.setContentType("text/rtf; charset=gb2312");
// jtp.setEditorKit(rtfeditor);
jep.setContentType("text/rtf; charset=gb2312");
jep.setEditorKit(rtfeditor);
try {
FileInputStream fis = new FileInputStream("D:/temp/myrtf.rtf");
// rtfeditor.read(fis, jtp.getDocument(), 0);
rtfeditor.read(fis, jep.getDocument(), 0);
} catch (FileNotFoundException e) {
System.err.println(e.toString());
e.printStackTrace();
} catch (IOException e) {
System.err.println(e.toString());
e.printStackTrace();
} catch (BadLocationException e) {
System.err.println(e.toString());
e.printStackTrace();
}
最后是实验的结果:
我用Word编辑了一段文字,包括一段英文、一段中文、一张图片、一个数学公式。
测试的结果是:除了英文外,其他都是乱码!!!!!!!!!!!!!!!!
- Java中存取Rtf文件
- Java中存取Rtf文件的程序
- Java中存取Rtf文件的程序
- java中.rtf文件变成文本文件
- Delphi中对Oracle存取RTF文档
- JAVA解析RTF 文件
- c#中读写rtf文件
- Delphi中对Oracle存取RTF文档(作者:苏涌)
- java存取密码于keystore文件中
- 数据库中存取文件
- 数据库中存取文件
- 数据库中存取文件
- 数据库中存取文件
- 数据库中存取文件
- 数据库中存取文件
- 数据库中存取文件
- 数据库中存取文件
- 数据库中存取文件
- 启航——10年前参加成人仪式时的涂鸦
- Eclipse快捷键大全
- 给爸爸的生日祝福
- Sysinternals Suite Build 20071109
- 反抗基本|
- Java中存取Rtf文件
- 数据结构习题学习笔记(The First Day)
- 利用IWebBrowser2接口的Navigate2方法实现Http POST传输
- Struts+Ajax实现定时刷新Table内容,实现在线人员显示
- JBPM外接任务表单简述
- google搜索原理论文2
- google搜索原理论文 1
- [转]如何在Web页面上直接打开、编辑、创建Office文档
- 惠普hw6965GPS技术简介