本文来自CSDN博客:
在Android中读取doc文件需要用第三方jar包tm-extractors-0.4.jar,读取的过程很简单
和普通的文件流操作基本一样,下面写一个简单的例子:
package 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){
// 创建输入流读取doc文件FileInputStream in;String text = null;try { in = new FileInputStream(new File(file));WordExtractor extractor = null;// 创建WordExtractorextractor = new WordExtractor();// 对doc文件进行提取text = extractor.extractText(in);} catch (FileNotFoundException e) { e.printStackTrace();} catch (Exception e) { e.printStackTrace();}return text;}}代码就这些,看起来也就是文件流操作,只是多加了一个jar包而已,doc文件放在模拟器sd卡的根目录, 目前还在优化处理,这种方法只能读取纯文本文件,如果文件中有图片的话就无法显示了。