Android 读取工程中的txt文件

来源:互联网 发布:js target.nodename 编辑:程序博客网 时间:2024/05/17 06:12

1.众所周知,Android的res文件夹是用来存储资源的,可以在res文件夹下建立一个raw文件夹,放置在raw文件夹下的内容会被原样打包,而不会被编译成二进制文件,并且可以通过R文件进行很方便地访问。

比如我们可以将更新信息、版权信息等放到txt文件中,然后放到raw文件中,然后很方便地进行访问。


在raw中放入一个a.txt文件,然后就可以在Activity中使用getResources().openRawResource(R.raw.a);方法获取一个此文件的InputStream类,而后就可以很方便地进行读写a.txt了。

[java] view plaincopy
  1. InputStream inputStream = getResources().openRawResource(R.raw.a);  

一个获取InputStream中字符串内容的方法:

[java] view plaincopy
  1. public static String getString(InputStream inputStream) {  
  2.     InputStreamReader inputStreamReader = null;  
  3.     try {  
  4.         inputStreamReader = new InputStreamReader(inputStream, "gbk");  
  5.     } catch (UnsupportedEncodingException e1) {  
  6.         e1.printStackTrace();  
  7.     }  
  8.     BufferedReader reader = new BufferedReader(inputStreamReader);  
  9.     StringBuffer sb = new StringBuffer("");  
  10.     String line;  
  11.     try {  
  12.         while ((line = reader.readLine()) != null) {  
  13.             sb.append(line);  
  14.             sb.append("\n");  
  15.         }  
  16.     } catch (IOException e) {  
  17.         e.printStackTrace();  
  18.     }  
  19.     return sb.toString();  
  20. }  
传入一个InputStream,返回其中的文本内容。

其中

[java] view plaincopy
  1. inputStreamReader = new InputStreamReader(inputStream, "gbk");  

为以gbk编码读取内容,不同的文本文件可能编码不同,如果出现乱码,可能需要调整编码

/-----------------------------------------------------------

2下面通过一个例子讲解读取资源文件显示在ScrollView当中: 

1.ReadAsset.java文件: 
Java代码  收藏代码
  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. }  

2. read_asset.xml文件 
Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView android:layout_width="fill_parent"  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_height="fill_parent" android:paddingTop="50dip">  
  5.     <TextView android:id="@+id/text" android:layout_width="fill_parent"  
  6.         android:layout_height="wrap_content" android:textStyle="normal" />  
  7. </ScrollView>  


3.然后在工程里面新建一个assets文件夹,随便放一个index.txt的文件在其中,运行 
  Ctrl+F11进行测试即可; 
0 0
原创粉丝点击