简体   繁体   English

当我尝试读取现有docx文件的内容时docx4j给出错误

[英]Docx4j gives error when I try to read content of existing docx file

I am trying to read the content of a docx file from my system using Docx4Java. 我试图使用Docx4Java从系统中读取docx文件的内容。 I have searched enough for the answer but unfortunately couldn't find one. 我已经搜索了足够的答案,但不幸的是找不到。

Below is the error I got while I tried to implement my code. 以下是我尝试实施代码时遇到的错误。

java.io.FileNotFoundException: G:\\WorkSpaces\\111.docx (The system cannot find the file specified) java.io.FileNotFoundException:G:\\ WorkSpaces \\ 111.docx(系统找不到指定的文件)

PS : There is no mistake in providing file path. PS :提供文件路径没有错误。 No jar file is absent. 没有jar文件。 I have checked everything before asking. 在询问之前,我已经检查了所有内容。

Can someone please tell me where am I going wrong ? 有人可以告诉我我要去哪里错吗?

import java.io.*;
import java.util.*;
import org.docx4j.*;

public class doc4jcodegeeks {
   public static void main(String[] args) throws FileNotFoundException {
    try {
        doc4jcodegeeks dcf = new doc4jcodegeeks();
        dcf.getTemplate();
        }
        catch (Exception e) {
        e.printStackTrace();
       }
    }

    private WordprocessingMLPackage getTemplate() throws Docx4JException, FileNotFoundException {
    WordprocessingMLPackage template = WordprocessingMLPackage.load(new FileInputStream(
            new File("G:\\WorkSpaces\\111.docx")));
    return template;
}

Seems to be G: is network disk. 似乎是G:是网络磁盘。 In windows JVM runs under System user. 在Windows中,JVM在“ System用户下运行。 This user can't see network disks. 该用户看不到网络磁盘。 You can try: 你可以试试:

  1. Change user, when you start your program; 启动程序时更改用户;

  2. Try to specify full network path ( \\\\share\\filename.docx ) 尝试指定完整的网络路径( \\\\share\\filename.docx

  3. At last copy file to local disk; 最后将文件复制到本地磁盘;

Thanks for your answer Ken Bekov. 感谢您的回答Ken Bekov。 After some time, I figured out the solution and displayed document's contents on output window in the following way : 一段时间后,我想出了解决方案,并通过以下方式在输出窗口中显示了文档的内容:

private WordprocessingMLPackage getTemplate() throws Docx4JException, FileNotFoundException {
    WordprocessingMLPackage template = WordprocessingMLPackage.load(new java.io.File("G:\\WorkSpaces\\111.docx"));
    MainDocumentPart documentPart = template.getMainDocumentPart();

    List<Object> listObj = documentPart.getContent();

     String str = listObj.toString();
     System.out.println(str);

    return template;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM