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

  1. package com.example.ReadAsset;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.TextView;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8.   
  9. public class ReadAsset extends Activity {  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.read_asset);  
  14.   
  15.         try {  
  16. //Return an AssetManager instance for your application's package  
  17.             InputStream is = getAssets().open("index.txt");  
  18.             int size = is.available();  
  19.   
  20.             // Read the entire asset into a local byte buffer.  
  21.             byte[] buffer = new byte[size];  
  22.             is.read(buffer);  
  23.             is.close();  
  24.   
  25.             // Convert the buffer into a string.  
  26.             String text = new String(buffer, "GB2312");  
  27.   
  28.             // Finally stick the string into the text view.  
  29.             TextView tv = (TextView) findViewById(R.id.text);  
  30.             tv.setText(text);  
  31.         } catch (IOException e) {  
  32.             // Should never happen!  
  33.             throw new RuntimeException(e);  
  34.         }  
  35.     }  
  36. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值