Android讀取Resources和Assets中的檔案

來源:互聯網
上載者:User

 在Android平台下,除了對應用程式的私人檔案夾中的檔案進行操作外,還可以從資源檔和 Assets 中獲得輸入資料流讀取資料,這些檔案分別放在應用程式的res/raw 目錄和 assets 目錄下,這些檔案在編譯的時候和其他檔案一起被打包。

    需要注意的是,來自Resources和Assets 中的檔案只可以讀取而不能進行寫的操作,下面就通過一個例子來說明如何從 Resources 和 Assets中的檔案中讀取資訊。首先分別在res/raw 和 assets 目錄下建立兩個文字檔 "test1.txt"   和 "test2.txt" 用以讀取,結構如。

   為了避免字串轉碼帶來的麻煩,可以將兩個文字檔的編碼格式設定為UTF-8。設定編碼格式的方法有很多種,比較簡單的一種是用 Windows 的記事本開啟文字檔,在另存新檔對話方塊中編碼格式選擇"UTF-8" ,如。

看一下運行後的效果。

下面我們就來看看代碼吧。
Activity02

Java代碼 {
function onclick()
{
dp.sh.Toolbar.CopyToClipboard(this);return false;
}
}" href="http://byandby.javaeye.com/blog/835744">
  1. package xiaohang.zhimeng;   
  2.   
  3. import java.io.InputStream;   
  4. import org.apache.http.util.EncodingUtils;   
  5. import android.app.Activity;   
  6. import android.graphics.Color;   
  7. import android.os.Bundle;   
  8. import android.widget.TextView;   
  9.   
  10. public class Activity02 extends Activity{   
  11.        
  12.     public static final String ENCODING = "UTF-8";   
  13.     TextView tv1;   
  14.     TextView tv2;   
  15.        
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {   
  18.         super.onCreate(savedInstanceState);   
  19.         setContentView(R.layout.main);   
  20.         tv1 = (TextView)findViewById(R.id.tv1);   
  21.         tv1.setTextColor(Color.RED);   
  22.         tv1.setTextSize(15.0f);   
  23.         tv2 = (TextView)findViewById(R.id.tv2);   
  24.         tv2.setTextColor(Color.RED);   
  25.         tv2.setTextSize(15.0f);   
  26.         tv1.setText(getFromRaw());   
  27.         tv2.setText(getFromAssets("test2.txt"));   
  28.     }   
  29.        
  30.     //從resources中的raw 檔案夾中擷取檔案並讀取資料   
  31.     public String getFromRaw(){   
  32.         String result = "";   
  33.             try {   
  34.                 InputStream in = getResources().openRawResource(R.raw.test1);   
  35.                 //擷取檔案的位元組數   
  36.                 int lenght = in.available();   
  37.                 //建立byte數組   
  38.                 byte[]  buffer = new byte[lenght];   
  39.                 //將檔案中的資料讀到byte數組中   
  40.                 in.read(buffer);   
  41.                 result = EncodingUtils.getString(buffer, ENCODING);   
  42.             } catch (Exception e) {   
  43.                 e.printStackTrace();   
  44.             }   
  45.             return result;   
  46.     }   
  47.        
  48.     //從assets 檔案夾中擷取檔案並讀取資料   
  49.     public String getFromAssets(String fileName){   
  50.         String result = "";   
  51.             try {   
  52.                 InputStream in = getResources().getAssets().open(fileName);   
  53.                 //擷取檔案的位元組數   
  54.                 int lenght = in.available();   
  55.                 //建立byte數組   
  56.                 byte[]  buffer = new byte[lenght];   
  57.                 //將檔案中的資料讀到byte數組中   
  58.                 in.read(buffer);   
  59.                 result = EncodingUtils.getString(buffer, ENCODING);   
  60.             } catch (Exception e) {   
  61.                 e.printStackTrace();   
  62.             }   
  63.             return result;   
  64.     }   
  65. }  

轉http://byandby.javaeye.com/blog/835744

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.