Android reads doc files

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.