gpt4 book ai didi

java - 我的一段代码正在尝试读取 doc/docx 文件。调用 WordExtractor 构造函数时出现歧义错误

转载 作者:行者123 更新时间:2023-11-29 03:14:05 25 4
gpt4 key购买 nike

我从网站上复制了下面的代码。它使用 Apache POI 包读取 Java 中的 .doc/.docx 文件。

WordExtractor we = new WordExtractor(doc); 给出以下错误:对 WordExtractor 的引用不明确。 WordExtractor 中的构造函数 WordExtractor(POIFSFileSystem) 和 WordExtractor 中的 WordExtractor(HWPFDocument) 匹配。

对于任何愚蠢的错误,我深表歉意,我是第一次阅读此 .doc 文档。谢谢你们 ! :)

代码:

package testdeployment;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
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;


/**
*
* @author Aishwarya
*/
public class MsFileReader {
public static void readDocFile(String fileName) {

try {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file.getAbsolutePath());

HWPFDocument doc = new HWPFDocument(fis);

WordExtractor we = new WordExtractor(doc);

String[] paragraphs = we.getParagraphText();

System.out.println("Total no of paragraph "+paragraphs.length);
for (String para : paragraphs) {
System.out.println(para.toString());
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}

}

public static void readDocxFile(String fileName) {

try {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file.getAbsolutePath());

XWPFDocument document = new XWPFDocument(fis);

List<XWPFParagraph> paragraphs = document.getParagraphs();

System.out.println("Total no of paragraph "+paragraphs.size());
for (XWPFParagraph para : paragraphs) {
System.out.println(para.getText());
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {

readDocxFile("C:\\Test.docx");

readDocFile("C:\\Test.doc");

}

最佳答案

你好,这段代码可以正常工作,你可以写这个,

public static void readDocxFile(String fileName) {

try {
File file = new File(fileName);
POIFSFileSystem fs = null;
fs = new POIFSFileSystem(new FileInputStream(file.getAbsolutePath()));
HWPFDocument doc = new HWPFDocument(fs);
readParagraphs(doc);

} catch (Exception e) {
e.printStackTrace();
}
}

public static void readParagraphs(HWPFDocument doc) throws Exception{
WordExtractor we = new WordExtractor(doc);

/**Get the total number of paragraphs**/
String[] paragraphs = we.getParagraphText();
System.out.println("Total Paragraphs: "+paragraphs.length);

for (int i = 0; i < paragraphs.length; i++) {

System.out.println("Length of paragraph "+(i +1)+": "+ paragraphs[i].length());
System.out.println(paragraphs[i].toString());

}

}

关于java - 我的一段代码正在尝试读取 doc/docx 文件。调用 WordExtractor 构造函数时出现歧义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27659223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com