【问题标题】:Toiling with HTMLEditorKit使用 HTMLEditorKit 工作
【发布时间】:2012-02-29 09:38:26
【问题描述】:

我是一名新手 Java 程序员,试图使用 HTMLEditorKit 库来遍历 HTML 文档并将其更改为我的链接(主要是为了好玩,我正在做的事情可以毫无问题地完成)

但我的问题是:在我修改了我的 HTML 文件后,我留下了一个 HTMLDocument,我不知道如何保存回 HTML 文件。

HTMLEditorKit kit = new HTMLEditorKit();
File file = new File("local file")
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument(); 
doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
InputStreamReader(url.openConnection().getInputStream()); 
FileReader HTMLReader = new FileReader(file); 
kit.read(HTMLReader, doc, 0); 

之后我用“doc”元素做我的事。

现在我已经完成了,我只想将它保存回来,最好覆盖我最初从中获取 HTML 的文件。

谁能告诉我之后如何将修改后的 HTML 文档保存到 html 文件中?

【问题讨论】:

    标签: java swing dom htmleditorkit


    【解决方案1】:

    您可以使用 HTMLEditorKit 类的write method。示例代码:

    FileWriter writer = new FileWriter("local file");
    try {
      kit.write(writer, doc, 0, doc.getLength());
    } finally {
      writer.close();
    }
    

    【讨论】:

    • 正是我想要的,谢谢! =)
    猜你喜欢
    • 2013-03-14
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多