温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android应用中如何实现读取项目中的txt文件

发布时间:2020-11-23 17:44:01 来源:亿速云 阅读:897 作者:Leah 栏目:移动开发

这篇文章给大家介绍Android应用中如何实现读取项目中的txt文件,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

具体如下:

1. 众所周知,Android的res文件夹是用来存储资源的,可以在res文件夹下建立一个raw文件夹,放置在raw文件夹下的内容会被原样打包,而不会被编译成二进制文件,并且可以通过R文件进行很方便地访问。
比如我们可以将更新信息、版权信息等放到txt文件中,然后放到raw文件中,然后很方便地进行访问。

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

InputStream inputStream = getResources().openRawResource(R.raw.a);

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

public static String getString(InputStream inputStream) {
  InputStreamReader inputStreamReader = null;
  try {
    inputStreamReader = new InputStreamReader(inputStream, "gbk");
  } catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
  }
  BufferedReader reader = new BufferedReader(inputStreamReader);
  StringBuffer sb = new StringBuffer("");
  String line;
  try {
    while ((line = reader.readLine()) != null) {
      sb.append(line);
      sb.append("\n");
    }
  } catch (IOException e) {
    e.printStackTrace();
  }
  return sb.toString();
}

传入一个InputStream,返回其中的文本内容。

其中:

inputStreamReader = new InputStreamReader(inputStream, "gbk");

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

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

①.ReadAsset.java文件:

package com.example.ReadAsset;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
public class ReadAsset extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.read_asset);
    try {
//Return an AssetManager instance for your application's package
      InputStream is = getAssets().open("index.txt");
      int size = is.available();
      // Read the entire asset into a local byte buffer.
      byte[] buffer = new byte[size];
      is.read(buffer);
      is.close();
      // Convert the buffer into a string.
      String text = new String(buffer, "GB2312");
      // Finally stick the string into the text view.
      TextView tv = (TextView) findViewById(R.id.text);
      tv.setText(text);
    } catch (IOException e) {
      // Should never happen!
      throw new RuntimeException(e);
    }
  }
}

②. read_asset.xml文件

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<ScrollView android:layout_width="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="fill_parent" android:paddingTop="50dip">
  <TextView android:id="@+id/text" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:textStyle="normal" />
</ScrollView>

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

关于Android应用中如何实现读取项目中的txt文件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI