android中资源是只读的,不可写。

下面我们来读取Assets目录和res/raw目录下的文本文件到TextView中,首先要做的就是将文件放入到这两个文件夹里

android读取Resources中内容_第1张图片

在activity_main.xml中放入两个TextView来显示文本内容

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.ssln.fileresources.MainActivity" >    <TextView        android:id="@+id/tvRaw"        android:layout_width="match_parent"        android:layout_height="wrap_content"       />    <TextView        android:id="@+id/tvAssets"        android:layout_width="match_parent"        android:layout_height="wrap_content"        /></LinearLayout>

然后在mainactivity.java中封装了2个方法,分别读取两个目录下的内容,一种是获取内容大小,然后直接读取到缓冲区,另外一种是逐行读取文件内容

package com.ssln.fileresources;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import org.apache.http.util.EncodingUtils;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {    private final String Encoding = "utf-8"; // 文件编码    private final String fileName = "text.txt"; // 文件名    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        TextView tvAssets = (TextView) findViewById(R.id.tvAssets);        TextView tvRaw = (TextView) findViewById(R.id.tvRaw);                tvAssets.setText(ReadFromAssets());                tvRaw.setText(ReadFromRaw());    }    /**     * 读取Assets目录下的文件     *      * @return     */    private String ReadFromAssets() {        String retStr = "";        try {            // 打开文件到流            InputStream inStream = getResources().getAssets().open(fileName);            // 获取长度            int fileLen = inStream.available();            // 放入缓冲区            byte[] buffer = new byte[fileLen];            inStream.read(buffer);            // 转码到文本            retStr = EncodingUtils.getString(buffer, Encoding);            //关闭            inStream.close();        } catch (IOException e) {            e.printStackTrace();        }            return retStr;    }    /**     * 读取Raw目录下文件     *      * @return     */    private String ReadFromRaw() {        String retStr="";        try        {            InputStream inStream=getResources().openRawResource(R.raw.text);            //这里我们使用另外一种读取方法,逐行读取内容            BufferedReader br=new BufferedReader(new InputStreamReader(inStream));            String tempStr="";            while((tempStr=br.readLine())!=null)            {                retStr+=tempStr+"\r\n";                Toast.makeText(this, tempStr, Toast.LENGTH_SHORT).show();            }        }        catch(IOException e)        {            e.printStackTrace();        }        return retStr;    }}

我们看下效果

android读取Resources中内容_第2张图片

??? 为毛是乱码尼?因为我们在上面读取的编码方式是utf-8的

private final String Encoding = "utf-8"; // 文件编码retStr = EncodingUtils.getString(buffer, Encoding);

肿么办?我们从新用记事本来存储下文件为utf-8就好了

android读取Resources中内容_第3张图片

在来看看效果

android读取Resources中内容_第4张图片

更多相关文章

  1. 在pc上安装android market软件并提取apk文件
  2. Android SDK Manager在添加Platforms和其他Packages时提示无法在
  3. 退出Activity时,保存edittext的内容
  4. Android Preference Summary中实时显示内容变更
  5. android raw读取超过1M文件的方法
  6. Android 资源文件夹下的文件不能有大写字符,会导致R.java无法生成

随机推荐

  1. Mysql 范式、索引、视图、事务、sp等概
  2. LINQ之开放式并发控制和事务
  3. MySQL查询过程中出现lost connection to
  4. 求指导,PB12.0 如何用ini文件 连接sql 200
  5. MySQL Cluster在线添加数据节点
  6. Windows安装MySQL Community 5.7.17(非msi
  7. 如何在SQL server中创建excel表并导出
  8. 数据库问题java.sql.SQLException: Colum
  9. Django学习之mysql增删改查
  10. 解决mysql sum求和返回null问题或IFNULL