android读取Resources中内容

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

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

在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+"
";
                Toast.makeText(this, tempStr, Toast.LENGTH_SHORT).show();
            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        return retStr;

    }
}

我们看下效果

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

private final String Encoding = "utf-8"; // 文件编码

retStr = EncodingUtils.getString(buffer, Encoding);

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

在来看看效果

原文地址:https://www.cnblogs.com/alwaysfirst/p/4009204.html