p
ackage
com.word.read;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
org.textmining.text.extraction.WordExtractor;
import
Android.app.Activity;
import
Android.os.Bundle;
import
Android.os.Environment;
import
Android.widget.TextView;
public
class
WordReader
extends
Activity {
/** Called when the activity is first created. */
private
TextView text;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.text);
String str = readWord(Environment.getExternalStorageDirectory().getAbsolutePath() +
"/baojinggong.doc"
);
text.setText(str.trim().replace(
"\r"
,
""
));
}
public
String readWord(String file){
//创建输入流用来<a title="读取doc文件" href="http://www.android-study.com/pingtaikaifa/38.html">读取doc文件</a>
FileInputStream in;
String text =
null
;
try
{
in =
new
FileInputStream(
new
File(file));
WordExtractor extractor =
null
;
//创建WordExtractor
extractor =
new
WordExtractor();
//进行提取对doc文件
text = extractor.extractText(in);
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
catch
(Exception e) {
e.printStackTrace();
}
return
text;
}
}
看这些代码,其实也就是文件流的操作,只不过是多加了一个jar包而已,word(doc)文件放在模拟器sd卡的根目录,运行后的效果如下: