利用JAVA读写rtf文档

今天尝试用JAVA来读写RTF文档,API中提供了类RTFEditorKit来对富文本的读写,类放在

javax.swing.text.rtf.RTFEditorKit,虽然放在swing下面,但却不是由swing团队编写的。
RTFEditorKit提供了两个主要方法来读写,分别是read和write;
例子中我用的是

read(InputStream in, Document doc, int pos)

write(OutputStream out, Document doc, int pos, int len)

import java.io.*;

import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.rtf.*;

public class AccessRTF {
	String text;
	DefaultStyledDocument dsd;
	RTFEditorKit rtf;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
      		AccessRTF readRTF=new AccessRTF();
		readRTF.readRtf(new File("e:\\1.rtf"));
		readRTF.writeRtf(new File("e:\\out.rtf"));
	}
	public void readRtf(File in) {
		rtf=new RTFEditorKit();
		dsd=new DefaultStyledDocument();
		try {
			rtf.read(new FileInputStream(in), dsd, 0);
			text = new String(dsd.getText(0, dsd.getLength());
			System.out.println(text);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	public void writeRtf(File out) {
		try {
			rtf.write(new FileOutputStream(out), dsd, 0, dsd.getLength());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Java 中,可以通过使用内置的 java.net 包来读取 HTML 文档。下面是一个简单的代码示例: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; public class ReadHTML { public static void main(String[] args) { try { URL url = new URL("https://www.example.com"); // 替换为要读取的网页地址 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); } catch (Exception e) { System.out.println(e.getMessage()); } } } ``` 这段代码通过打开一个 URL 连接,然后使用 BufferedReader 对象来读取网页内容。最后,它将每一行内容打印到控制台。 如果你想要写入 HTML 文档,可以使用 JavaFileWriter 或者 BufferedWriter 类。具体实现可以参考以下代码示例: ```java import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class WriteHTML { public static void main(String[] args) { try { String content = "<html><body><h1>Hello, world!</h1></body></html>"; // 要写入的内容 File file = new File("output.html"); // 文件名 FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } } } ``` 这段代码将一个包含 HTML 代码的字符串写入到一个名为 `output.html` 的文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值