android 文件讀取(assets)


assets文件夾資源的訪問

       assets文件夾里面的文件都是保持原始的文件格式,需要用AssetManager以字節流的形式讀取文件。
      1. 先在Activity里面調用getAssets() 來獲取AssetManager引用。
      2. 再用AssetManager的open(String fileName, int accessMode) 方法則指定讀取的文件以及訪問模式就能得到輸入流InputStream。 
      3. 然后就是用已經open file 的inputStream讀取文件,讀取完成后記得inputStream.close() 。
      4.調用AssetManager.close() 關閉AssetManager。

需要注意的是,來自Resources和Assets 中的文件只可以讀取而不能進行寫的操作
以下為從Raw文件中讀取:
代碼

 
   public String getFromRaw(){ 
            try { 
                InputStreamReader inputReader = new InputStreamReader( getResources().openRawResource(R.raw.test1));
                BufferedReader bufReader = new BufferedReader(inputReader);
                String line="";
                String Result="";
                while((line = bufReader.readLine()) != null)
                    Result += line;
                return Result;
            } catch (Exception e) { 
                e.printStackTrace(); 
            }             
    } 
以下為直接從assets讀取
代碼
    public String getFromAssets(String fileName){ 
            try { 
                 InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) ); 
                BufferedReader bufReader = new BufferedReader(inputReader);
                String line="";
                String Result="";
                while((line = bufReader.readLine()) != null)
                    Result += line;
                return Result;
            } catch (Exception e) { 
                e.printStackTrace(); 
            }
    } 
當然如果你要得到內存流的話也可以直接返回內存流!
接下來,我們新建一個工程文件,命名為AssetsDemo。

然后建立一個布局文件,如下,很簡單,無需我多介紹,大家一看就明白。

然后呢,我從網上找了段文字,存放在assets文件目錄下,取名為health.txt 這就是今天我們要讀取的文件啦。

這個.txt文件,我們可以直接雙擊查看。如下所示。

接下來,就是今天的重頭戲,Android讀取文件的核心代碼。就直接貼代碼了。
package com.assets.cn;
import java.io.InputStream;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class AssetsDemoActivity extends Activity {
public static final String ENCODING = "UTF-8";
TextView tv1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
tv1 = (TextView)findViewById(R.id.tv1);
tv1.setTextColor(Color.BLACK);
tv1.setTextSize(25.0f);
tv1.setText(getFromAssets("health.txt"));
}

//從assets 文件夾中獲取文件並讀取數據
public String getFromAssets(String fileName){
   String result = "";
   try {
InputStream in = getResources().getAssets().open(fileName);
//獲取文件的字節數
int lenght = in.available();
//創建byte數組
byte[]  buffer = new byte[lenght];
//將文件中的數據讀到byte數組中
in.read(buffer);
result = EncodingUtils.getString(buffer, ENCODING);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}


這里是mainfest文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.assets.cn"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AssetsDemoActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>
最后,我們運行一下程序。

源碼:
http://files.cnblogs.com/greatverve/AssetsDemo.zip
太忙,抽時間研究下sgf讀寫。
url: http://greatverve.cnblogs.com/archive/2012/03/07/android-assets.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM