在没有第三方库的Java中将RTF转换为Java中的DOCX文件

发布于 2025-01-22 15:19:03 字数 668 浏览 5 评论 0原文

是否有任何方法可以将RTF格式的文件转换为DOCX格式,而没有第三方库,例如Aspose等。我尝试使用InputStream和outputStream,但会导致损坏的DOCX文件。

private static void convertToDoc() throws IOException {
        String inputPath = "C:\\inputFile.rtf";
        String OutputPath = "C:\\outputFile.docx";
        File initialFile = new File(inputPath);
        InputStream source = new FileInputStream(initialFile);
        OutputStream target = new FileOutputStream(OutputPath);
        byte[] buf = new byte[8192];
        int length;
        while ((length = source.read(buf)) > 0) {
            target.write(buf, 0, length);
        }
        source.close();
        target.close();
    }

Is there any way to convert a file in rtf format to docx format without third party libraries such as aspose etc. I tried to use InputStream and OutputStream but it leads to a corrupted docx file.

private static void convertToDoc() throws IOException {
        String inputPath = "C:\\inputFile.rtf";
        String OutputPath = "C:\\outputFile.docx";
        File initialFile = new File(inputPath);
        InputStream source = new FileInputStream(initialFile);
        OutputStream target = new FileOutputStream(OutputPath);
        byte[] buf = new byte[8192];
        int length;
        while ((length = source.read(buf)) > 0) {
            target.write(buf, 0, length);
        }
        source.close();
        target.close();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文