在java中读取.docx文件

在java中读取.docx文件,java,apache-poi,docx,docx4j,Java,Apache Poi,Docx,Docx4j,我正在尝试用java读取一个文件,代码如下: public void readFile(String fileName){ try { BufferedReader reader= new BufferedReader(new FileReader(fileName)); String line=null; while((line=reader.readLine()) != null ){ System.o

我正在尝试用java读取一个文件,代码如下:

public void readFile(String fileName){
        try {
        BufferedReader reader= new BufferedReader(new FileReader(fileName)); 
        String line=null;
        while((line=reader.readLine()) != null ){
            System.out.println(line);
        }
        }catch (Exception ex){}
            }

它在txt文件的情况下工作正常。然而,对于docx文件,它正在打印奇怪的字符。如何读取Java中的.docx文件。

您不能直接读取docx文件或doc文件。您需要有一个API来读取word文件。使用ApachePOI。如果您有任何疑问,请参考stackoverflow.com上的此帖子
内部.docx文件被组织为压缩的XML-文件,而.doc是一种二进制文件格式。所以你不能直接阅读其中任何一个。请查看docx4japachepoi

如果您试图创建或操作.docx文件,请尝试docx4j


或者选择apachePOI

您必须拥有以下6罐:

  • xmlbeans-2.3.0.jar
  • dom4j-1.6.1.jar
  • poi-ooxml-3.8-20120326.jar
  • poi-ooxml-schemas-3.8-20120326.jar
  • poi-scratchpad-3.2-FINAL.jar
  • poi-3.5-FINAL.jar
  • 代码:

    导入java.io.File;
    导入java.io.FileInputStream;
    导入java.util.Iterator;
    导入java.util.List;
    导入org.apache.poi.hwpf.hwpf文档;
    导入org.apache.poi.hwpf.extractor.WordExtractor;
    导入org.apache.poi.xwpf.usermodel.XWPFDocument;
    导入org.apache.poi.xwpf.usermodel.XWPFParagraph;
    公开课考试{
    公共静态void readDocxFile(字符串文件名){
    试一试{
    文件=新文件(文件名);
    FileInputStream fis=新的FileInputStream(file.getAbsolutePath());
    XWPF文件=新的XWPF文件(fis);
    对于(inti=0;i
    import java.io.File;
    导入java.io.FileInputStream;
    导入java.util.List;
    导入org.apache.poi.xwpf.usermodel.XWPFDocument;
    导入org.apache.poi.xwpf.usermodel.XWPFParagraph;
    public void readDocxFile(){
    试一试{
    File File=新文件(“C:/NetBeans Output/documentx.docx”);
    FileInputStream fis=新的FileInputStream(file.getAbsolutePath());
    XWPF文件=新的XWPF文件(fis);
    列表段落=document.getPages();
    对于(XWPF段落:段落){
    System.out.println(para.getText());
    }
    fis.close();
    }捕获(例外e){
    e、 printStackTrace();
    }
    }
    
    似乎是用于读取Microsoft文件格式的最常用库。对于Excel(.xls和.xlsx)来说,这当然是正确的
    .docx
    文件的可能副本不是纯文本文件,它们的文件扩展名为
    .txt
    ;并且它们的编码方式不同。正如上面@jahroy所建议的,您需要一个API来读取它。您的代码将不会编译。段落未定义
    import java.io.File;
    import java.io.FileInputStream;
    import java.util.Iterator;
    import java.util.List;
    import org.apache.poi.hwpf.HWPFDocument;
    import org.apache.poi.hwpf.extractor.WordExtractor;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.usermodel.XWPFParagraph;
    
    public class test {
     public static void readDocxFile(String fileName) {
    try {
    File file = new File(fileName);
    FileInputStream fis = new FileInputStream(file.getAbsolutePath());
    XWPFDocument document = new XWPFDocument(fis);
    for(int i=0;i<paragraphs.size();i++){
        System.out.println(paragraphs.get(i).getParagraphText());
    }
    fis.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    public static void main(String[] args) {
     readDocxFile("C:\\Users\\sp0c43734\\Desktop\\SwatiPisal.docx");
     }
    } 
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.util.List;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.usermodel.XWPFParagraph;
        public void readDocxFile() {
                try {
                    File file = new File("C:/NetBeans Output/documentx.docx");
                    FileInputStream fis = new FileInputStream(file.getAbsolutePath());
    
                    XWPFDocument document = new XWPFDocument(fis);
    
                    List<XWPFParagraph> paragraphs = document.getParagraphs();
    
    
                    for (XWPFParagraph para : paragraphs) {
                        System.out.println(para.getText());
                    }
                    fis.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }