这篇文章主要介绍了 Android 读取 assets 和 raw 文件内容的相关资料, 并附简单实例代码,需要的朋友可以参考下
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
android 之文件操作——读取 assets 和 raw 文件下的内容
1. 分别创建 assets 文件夹和 res/raw 文件夹:(要注意的 raw 文件是在 res 下 new,然后创建一个名字为 raw 的文件夹)
2. 创建两个 txt 文件,复制到 asset 和 raw 文件夹中:
3. 实现的效果:
4. 实现代码:
(1)布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <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:orientation="vertical"
- tools:context="base.readassetsfile.MainActivity">
- <Button
- android:textSize="20sp"
- android:text="@string/aasets_txt"
- android:id="@+id/readFile"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- <Button
- android:textSize="20sp"
- android:text="@string/raw"
- android:id="@+id/readRawFile"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
(2)具体实现:
- package base.readassetsfile;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.EditText;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.UnsupportedEncodingException;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- findViewById(R.id.readFile).setOnClickListener(this);
- findViewById(R.id.readRawFile).setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()){
- case R.id.readFile:
- readAsset();
- break;
- case R.id.readRawFile:
- readRaw();
- break;
- }
- }
- public void readAsset(){
- try {
- //获取文件中的字节
- InputStream inputStream=getResources().getAssets().open("Test.txt");
- //将字节转换为字符
- InputStreamReader isReader=new InputStreamReader(inputStream,"UTF-8");
- //使用bufferReader去读取内容
- BufferedReader reader=new BufferedReader(isReader);
- String out="";
- while((out=reader.readLine())!=null){
- Log.d("读取到的文件信息:",out);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void readRaw(){
- try {
- //获取文件中的内容
- InputStream inputStream=getResources().openRawResource(R.raw.test);
- //将文件中的字节转换为字符
- InputStreamReader isReader=new InputStreamReader(inputStream,"UTF-8");
- //使用bufferReader去读取字符
- BufferedReader reader=new BufferedReader(isReader);
- String out="";
- try {
- while((out=reader.readLine())!=null){
- Log.d("从raw文件夹中读取到的数据:",out);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
- }
来源: http://www.phperz.com/article/17/0316/302433.html