JAVA有什么好的方法可以将word里的文本读取出来

如题所述

你用免费版的Free Spire.Doc for Java可以直接读取Word文档里面的文本,参考代码:

import com.spire.doc.Document;

import java.io.FileWriter;

import java.io.IOException;

 

public class ExtractText {

 

    public static void main(String[] args) throws IOException {

 

        //加载Word文档

        Document document = new Document();

        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");

 

        //获取文档中的文本保存为String

        String text=document.getText();

 

        //将String写入Txt文件

        writeStringToTxt(text,"ExtractedText.txt");

    }

 

    public static void writeStringToTxt(String content, String txtFileName) throws IOException {

 

        FileWriter fWriter= new FileWriter(txtFileName,true);

        try {

            fWriter.write(content);

        }catch(IOException ex){

            ex.printStackTrace();

        }finally{

            try{

                fWriter.flush();

                fWriter.close();

            } catch (IOException ex) {

                ex.printStackTrace();

            }

        }

    }

}

参考自官网原文

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-12-14
很明显要用到I/O流
自己看看这部分吧

如果读写excel文件时候
要用到jxl.jar自己要下载这个jar文件
在查下怎么用本回答被提问者采纳
相似回答