发布网友
共1个回答
热心网友
public static void main(String[] args){
int i = 0;
File file = new File("F:/test.doc");
try{
FileInputStream fis = new FileInputStream(file);
WordExtractor wordExtractor = new WordExtractor(fis);
System.out.println("【提取的Word文件的内容如下所示:】");
System.out.println("");
String[] paragraph = wordExtractor.getParagraphText();
for(i=0;i<paragraph.length;i++)
{
String getTheParagraph=paragraph[i];
System.out.println("<第 "+(i+1)+"段的内容为:>");
System.out.print(getTheParagraph);
System.out.println("");
}
System.out.println("该Word文件共有"+paragraph.length+"段。");
}catch (FileNotFoundException e){
System.out.println("抱歉!找不到指定的文件!");
}catch (IOException e){
e.printStackTrace();
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("此Word文档共 "+i+"段!");
}
}
这个需要导入其它jar包,是apache的POI,你可以到网上搜一下。
以下是这个类用到的jar包:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hwpf.extractor.WordExtractor;