读取Resources和Assets中的文件

在Android平台上,可以从资源文件和assets中获得输入流读取数据。这些文件分别存放在应用程序的res/raw目录和assets目录下,这些文件将会在编译的时候和其他文件一起被打包。assets:不会在R.java文件下生成相应的标记,assets文件夹可以自己创建文件夹,必须使用AssetsManager类进行访问,存放到这里的资源在运行打包的时候都会打入程序安装包中。res/raw:该目录下的文件可以直接复制到设备上,不能有子文件夹,编译软件时,这里的数据不需要编译,直接加入到程序安装包中,使用方法是getResource().OpenRawResources(ID),其中参数ID的形式是R.raw.XXX.

需要注意的是:来自Resources和Assets中的文件只可以读取而不能够进行写操作。res/raw和assets文件夹来存放不需要系统编译成二进制的文件,例如字体文件等。




java代码:

package com.example.zhangying.respurcesandassets;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    public static final String ENCODING = "UTF-8"; //常量,代表编码格式
    private TextView tv1;//tv1的显示内容设置为Resource中的raw文件夹的文件
    private TextView tv2;//tv2的显示内容设置为Asset中的文件

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv1.setText(getFromRaw("test1.txt"));
        tv2.setText(getFromAsset("test2.txt"));
    }

    /**
     * resource中的raw文件夹中获取文件并读取数据
     * @param fileName
     * @return
     */
    public String getFromRaw(String fileName){
        String result = "";
        InputStream in = getResources().openRawResource(R.raw.test1);//Resourcesraw中的文件获取输入流
        try {
            int length = in.available();//获取文件的字节数
            byte[] buffer = new byte[length];//创建byte数组
            in.read(buffer);//将文件中的数据读取到byte数组中
            result = new String(buffer,ENCODING);//byte数组转换成指定格式的字符串
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * asset中获取文件并读取数据
     * @param fileName
     * @return
     */
    public String getFromAsset(String fileName){
        String result = "";
        try {
            InputStream in = getResources().getAssets().open(fileName);//Assets中的文件获取输入流
            int length = in.available();                           //获取文件的字节数
            byte [] buffer = new byte[length];                      //创建byte数组
            in.read(buffer);                                    //将文件中的数据读取到byte数组中
            result=new String(buffer,ENCODING);         //byte数组转换成指定格式的字符串
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
运行结果:
 
 

猜你喜欢

转载自blog.csdn.net/zhangying1994/article/details/50900676