您的位置:首页 > 其它

如何读取assets文件夹中的txt文件

2016-05-30 13:48 447 查看
转自:http://edison-cool911.iteye.com/blog/695145
Java代码  


package com.example.ReadAsset;  

  

import android.app.Activity;  

import android.os.Bundle;  

import android.widget.TextView;  

import java.io.IOException;  

import java.io.InputStream;  

  

public class ReadAsset extends Activity {  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.read_asset);  

  

        try {  

//Return an AssetManager instance for your application's package  

            InputStream is = getAssets().open("index.txt");  

            int size = is.available();  

  

            // Read the entire asset into a local byte buffer.  

            byte[] buffer = new byte[size];  

            is.read(buffer);  

            is.close();  

  

            // Convert the buffer into a string.  

            String text = new String(buffer, "GB2312");  

  

            // Finally stick the string into the text view.  

            TextView tv = (TextView) findViewById(R.id.text);  

            tv.setText(text);  

        } catch (IOException e) {  

            // Should never happen!  

            throw new RuntimeException(e);  

        }  

    }  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: