Android如何讀取doc檔案

來源:互聯網
上載者:User

在Android中讀取doc檔案需要用第三方jar包tm-extractors-0.4.jar,讀取的過程很簡單

和普通的檔案流操作基本一樣,下面寫一個簡單的例子:

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;        @Overridepublic 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){  // 建立輸入資料流讀取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;  }  }

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

代碼就這些,看起來也就是檔案流操作,只是多加了一個jar包而已,doc檔案放在模擬器sd卡的根目錄,運行後的效果如下:、

目前還在最佳化處理,這種方法只能讀取純文字檔案,如果檔案中有圖片的話就無法顯示了。

本文來自CSDN部落格:http://blog.csdn.net/huweilong1030/archive/2011/03/12/6242666.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.