发布于 2017-09-23 21:14:17 | 193 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android编程实现读取工程中的txt文件功能,结合实例形式详细分析了Android读取txt文件的原理、操作步骤与相关实现技巧,需要的朋友可以参考下

本文实例讲述了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文件


<?xml version="1.0" encoding="utf-8"?>
<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程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务