接到一个任务,要读取doc文件的内容。解析里面的内容,进行一个处理和返回。
读取doc:
看了
很多文章,基本都是:
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class WordImportUtils {
public static String readWordFile(String path) {
File file = new File(path);
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream((file.getAbsolutePath()));
HWPFDocument document = new HWPFDocument(fileInputStream);
WordExtractor extractor = new WordExtractor(document);
return extractor.getText();//此处还有很多别的方法可以使用
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fileInputStream != null){
try {
fileInputStream.close();
}catch (Exception e){