[java]
package Java.se.lucene;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
public class WordToTxt {
//創建字符串緩沖區
/*
* 不能寫成StringBuffer stringBuffer=null;
* 否則會報空指針異常
*/
StringBuffer stringBuffer=new StringBuffer();
//轉換word
public String readWord()
{
//word文檔路徑
String pathword="F:\\lucene\\doc\\1.doc";
try {
//創建存儲word文檔的對象
HWPFDocument doc = new HWPFDocument(new FileInputStream(pathword));
//用來獲得word文檔內容
Range range=doc.getRange();
//文檔段落數目
int paragraphCount=range.numParagraphs();
//遍歷段落讀取數據
for(int i=0;i<paragraphCount;i++)
{
Paragraph pph=range.getParagraph(i);
stringBuffer.append(pph.text());
System.out.println(stringBuffer.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuffer.toString().trim();
}
public static void main(String[] args) {
new WordToTxt().readWord();
}
}