当前位置: 技术问答>java相关
如何在Java简单的实现各种文档格式的转换
来源: 互联网 发布时间:2014-12-29
本文导语: 例如 txtrtf,rtfhtml,txtxml,txtdoc | import javax.swing.* ; import javax.swing.text.* ; import javax.swing.text.rtf.* ; import java.io.* ; public class RTFExample extends JFrame { RTFEditorKit editorKit = new RTFEditorKit() ; JTextP...
例如 txtrtf,rtfhtml,txtxml,txtdoc
|
import javax.swing.* ;
import javax.swing.text.* ;
import javax.swing.text.rtf.* ;
import java.io.* ;
public class RTFExample extends JFrame
{
RTFEditorKit editorKit = new RTFEditorKit() ;
JTextPane textPane = new JTextPane() ;
public static void main(String[] args)
{
RTFExample rtfExample = new RTFExample() ;
rtfExample.setSize(400,400) ;
rtfExample.setVisible(true) ;
}
public RTFExample()
{
super() ;
getContentPane().add(textPane) ;
textPane.setEditorKit(editorKit) ;
try
{
editorKit.read(new FileInputStream("c:\test.rtf"), textPane.getDocument(),0) ;
} catch (Exception e) { System.out.println(e) ;}
}
}
import javax.swing.text.* ;
import javax.swing.text.rtf.* ;
import java.io.* ;
public class RTFExample extends JFrame
{
RTFEditorKit editorKit = new RTFEditorKit() ;
JTextPane textPane = new JTextPane() ;
public static void main(String[] args)
{
RTFExample rtfExample = new RTFExample() ;
rtfExample.setSize(400,400) ;
rtfExample.setVisible(true) ;
}
public RTFExample()
{
super() ;
getContentPane().add(textPane) ;
textPane.setEditorKit(editorKit) ;
try
{
editorKit.read(new FileInputStream("c:\test.rtf"), textPane.getDocument(),0) ;
} catch (Exception e) { System.out.println(e) ;}
}
}
|
//我想先从。DOC中读入,在写入TXT中。
//下面的程序没有调试过。
//你可以自己调试以下。
//希望对你有用。
File myfile = new File("c:\testing.doc");
try
{
myrfile= new RandomAccessFile(myfile,"r");
String fileContent, asciiContent;
Character lineContent;
//read the text form file content line by line while((fileContent = myrfile.readLine())!=null)
{
while((lineContent=fileContent.readChar())!=null){
//now check each character whether it is printable or not
if (lineContent.isPrintable.. =="n")//check for the syntax
System.out.print(lineContent);
}
}// end of while
//close the file after use
myrfile.close();
}
//下面的程序没有调试过。
//你可以自己调试以下。
//希望对你有用。
File myfile = new File("c:\testing.doc");
try
{
myrfile= new RandomAccessFile(myfile,"r");
String fileContent, asciiContent;
Character lineContent;
//read the text form file content line by line while((fileContent = myrfile.readLine())!=null)
{
while((lineContent=fileContent.readChar())!=null){
//now check each character whether it is printable or not
if (lineContent.isPrintable.. =="n")//check for the syntax
System.out.print(lineContent);
}
}// end of while
//close the file after use
myrfile.close();
}