源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android 读取assets和raw文件内容实例代码

  • 时间:2022-09-27 07:14 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android 读取assets和raw文件内容实例代码
[b]android之文件操作——读取assets和raw文件下的内容[/b] 1.分别创建assets文件夹和res/raw文件夹:(要注意的raw文件是在res下new,然后创建一个名字为raw的文件夹)       [img]http://files.jb51.net/file_images/article/201610/2016102482924308.png?201692482944[/img] [img]http://files.jb51.net/file_images/article/201610/2016102482948247.png?20169248302[/img] 2.创建两个txt文件,复制到asset和raw文件夹中: [img]http://files.jb51.net/file_images/article/201610/2016102483009445.png?201692483022[/img] 3.实现的效果: [img]http://files.jb51.net/file_images/article/201610/2016102483057488.png?201692483110[/img] 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();
    }
  }

}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部