java中
lucene解析word工具类(读取word文档并查询数据)的方法
packageextract;
importjava.io.*;
importorg.textmining.text.extraction.WordExtractor;
public
classExtractorWord {
/**
* @param args
*/
publicstaticString getText(String file){
String s="";
String wordfile=file;
WordExtractor extractor=null;
try{
FileInputStream in=newFileInputStream(newFile(wordfile));
extractor=newWordExtractor();
s=extractor.extractText(in);
}catch(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
returns;
}
publicstaticvoidtoTextFile(String doc,String filename)throwsException{
String s="";
String wordfile=doc;
String txtfile=filename;
WordExtractor extractor=null;
try{
s=getText(wordfile);
PrintWriter pw=newPrintWriter(newFileWriter(newFile(filename)));
pw.write(s);
pw.flush();
pw.close();
System.out.print("成功写入文件!");
}catch(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
publicstaticvoidmain(String[] args) {
// TODO Auto-generated method stub
try{
String sc=getText("D:/workspace/testsearch2/htmls/ddd.doc");
System.out.print(sc);
toTextFile("D:/workspace/testsearch2/htmls/ddd.doc","D:/workspace/testsearch2/htmls/ddd.txt");
}catch(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
转 http://www.ablanxue.com/prone_3331_1.html