android新人在线求教如何用tm-extractors-0.4读取word文档
各位大哥帮帮忙吧!
下列程序用java已经成功运行了,可以读出word文件并显示:
package assets;
import java.io.File;
import java.io.FileInputStream;
import org.textmining.text.extraction.WordExtractor;
public class WordReader {
public static String readDoc(String doc) throws Exception {
// 创建输入流读取doc文件
FileInputStream in = new FileInputStream(new File(doc));
WordExtractor extractor = null;
String text = null;
// 创建WordExtractor
extractor = new WordExtractor();
// 对doc文件进行提取
text = extractor.extractText(in);
return text;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
String text = WordReader.readDoc("E:/Strust.doc");
System.out.println(text);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
但是如果在android中运行,怎么写WordReader.readDoc("E:/Strust.doc");
这个路径呢?如果写成绝对路径,在手机里面是没有意义的。现在求教如何写成相对路径?
我的Strust.doc放在项目的assets文件夹下。
[解决办法]
只有看日志才能解决问题了
我这边已经操作成功了:
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() + "/baojingong.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;
// 创建WordExtractor
extractor = new WordExtractor();
// 对doc文件进行提取
text = extractor.extractText(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return text;
}
就这些代码,文件放在模拟器sd卡的根目录,读出来的效果如下: