Reading DOC files in Android requires a third-party jar package tm-extractors-0.4.jar, the reading process is very simple
Similar to common file stream operations, the following is a simple example:
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 ){
// Create an input stream to read the DOC file
Fileinputstream in;
String text = NULL;
Try {
In = new fileinputstream (new file (File ));
Wordextractor extractor = NULL;
// Create a wordextractor
Extractor = new wordextractor ();
// Extract the DOC file
TEXT = extractor. extracttext (in );
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (exception e ){
E. printstacktrace ();
}
Return text;
}
}
The code looks like a file stream operation, but adds a jar package. The doc file is placed in the root directory of the SD card of the simulator. The following figure shows the effect after running :,
Currently, the processing is still being optimized. This method can only read plain text files and cannot be displayed if there are images in the files.