Java中存取Rtf文件的程序
2008-04-09 21:18
471 查看
狂找了几天在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编辑了一段文字,包括一段英文、一段中文、一张图片、一个数学公式。
测试的结果是:除了英文外,其他都是乱码!!!!!!!!!!!!!!!!
该文章转载自网络大本营:http://www.xrss.cn/Dev/JAVA/2007111217579.Html
本来不想写总结的,不过想 到这几天的辛苦,还是决定把找到的一点小东东写下来,希望能对以后的应用有所启迪。
下面是我找到的唯一可用的一段代码(使用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编辑了一段文字,包括一段英文、一段中文、一张图片、一个数学公式。
测试的结果是:除了英文外,其他都是乱码!!!!!!!!!!!!!!!!
该文章转载自网络大本营:http://www.xrss.cn/Dev/JAVA/2007111217579.Html
相关文章推荐
- Java中存取Rtf文件的程序
- java使用资源文件实现程序的国际化
- 分享非常有用的Java程序 (关键代码) (二)---JavaSript获取上传文件路径的文件名
- java 程序生成的文件,中文不显示,显示方框等问题,linux 安装中文字体..
- 把Java程序打包成jar文件包并执行
- Java桌面程序打包成exe可执行文件
- 如何利用iText在java程序中生成PDF文件
- java26行代码实现复制文件程序
- Java程序如何生成Jar、exe及安装文件
- 彻底摆脱配置文件 五(基于javaagent实现zkconfigutil对程序零侵入)
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- java配置文件或者eclipse中url等使用造成程序报错问题
- Java程序如何生成Jar、exe及安装文件
- 今天写了一个含配置文件的 文件分割 及 合并 的java程序。
- java 程序中打开文件和文件夹
- 利用Java编写HTML文件分析程序
- Java桌面应用安装部署、安装程序制作(exe4j + Inno Setup)——之三:使用exe4j生成EXE可执行文件
- java程序的递归算法,列出某个目录下的所有子目录和文件
- Web应用中的普通java程序获取资源文件
- 统计文本文件中单词出现频率,自己编写的Java小程序