天天看點

Java中存取Rtf檔案的程式

???句???澶╁??ava涓?瀛???Rtf??浠剁???規?锛?缁??????界?ㄤ袱涓?瀛?褰㈠?光???????鳳?锛?锛?

???ヤ??沖???葷???锛?涓?杩????拌???澶╃??杈????杩????沖?????懼?扮??涓??瑰?涓?涓???涓??ワ?甯????藉?逛互????搴??ㄦ??????杩???

<script src='Http://code.xrss.cn/AdJs/csdntitle.Js'></script>

涓??㈡?????懼?扮????涓????ㄧ??涓?娈典唬??(浣跨??ava涓???甯???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 );

? }

}

?ヤ??ユ??????宸辯??娴?璇?浠g??锛?

? 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();

? }

??????瀹?楠???缁???锛?

????ord缂?杈?浜?涓?娈墊??瀛?锛?????涓?娈佃?辨????涓?娈典腑????涓?寮??劇????涓?涓??闆????寮???

娴?璇???缁?????锛??や??辨??澶?锛??朵??芥??涔辯??锛?锛?锛?锛?锛?锛?锛?锛?锛?锛?锛?锛?锛?锛?锛?锛?

<script src='Http://code.xrss.cn/AdJs/csdnEnd.Js'></script>

璇ユ??绔?杞?杞借??缃?缁?澶ф???ワ?http://www.xrss.cn/Dev/JAVA/2007111217579.Html