乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      android 讀取Resources 和 Assets 中的文件

       昵稱8022812 2011-10-30
      在Android平臺下,除了對應(yīng)用程序的私有文件夾中的文件進(jìn)行操作外,還可以從資源文件和 Assets 中獲得輸入流讀取數(shù)據(jù),這些文件分別放在應(yīng)用程序的res/raw 目錄和 assets 目錄下,這些文件在編譯的時(shí)候和其他文件一起被打包。

          需要注意的是,來自Resources和Assets 中的文件只可以讀取而不能進(jìn)行寫的操作,下面就通過一個(gè)例子來說明如何從 Resources 和 Assets中的文件中讀取信息。首先分別在res/raw 和 assets 目錄下新建兩個(gè)文本文件 "test1.txt"   和 "test2.txt" 用以讀取,結(jié)構(gòu)如下圖。



         為了避免字符串轉(zhuǎn)碼帶來的麻煩,可以將兩個(gè)文本文件的編碼格式設(shè)置為UTF-8。設(shè)置編碼格式的方法有很多種,比較簡單的一種是用 Windows 的記事本打開文本文件,在另存為對話框中編碼格式選擇"UTF-8" ,如下圖。



      看一下運(yùn)行后的效果。




      下面我們就來看看代碼吧。
      Activity02
      Java代碼 復(fù)制代碼 收藏代碼
      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 文件夾中獲取文件并讀取數(shù)據(jù)   
      31.     public String getFromRaw(){   
      32.         String result = "";   
      33.             try {   
      34.                 InputStream in = getResources().openRawResource(R.raw.test1);   
      35.                 //獲取文件的字節(jié)數(shù)   
      36.                 int lenght = in.available();   
      37.                 //創(chuàng)建byte數(shù)組   
      38.                 byte[]  buffer = new byte[lenght];   
      39.                 //將文件中的數(shù)據(jù)讀到byte數(shù)組中   
      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 文件夾中獲取文件并讀取數(shù)據(jù)   
      49.     public String getFromAssets(String fileName){   
      50.         String result = "";   
      51.             try {   
      52.                 InputStream in = getResources().getAssets().open(fileName);   
      53.                 //獲取文件的字節(jié)數(shù)   
      54.                 int lenght = in.available();   
      55.                 //創(chuàng)建byte數(shù)組   
      56.                 byte[]  buffer = new byte[lenght];   
      57.                 //將文件中的數(shù)據(jù)讀到byte數(shù)組中   
      58.                 in.read(buffer);   
      59.                 result = EncodingUtils.getString(buffer, ENCODING);   
      60.             } catch (Exception e) {   
      61.                 e.printStackTrace();   
      62.             }   
      63.             return result;   
      64.     }   
      65. }  

        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多