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.