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.