亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android 讀取assets和raw文件內(nèi)容實例代碼

 更新時間:2016年10月24日 08:31:51   投稿:lqh  
這篇文章主要介紹了Android 讀取assets和raw文件內(nèi)容的相關(guān)資料,并附簡單實例代碼,需要的朋友可以參考下

android之文件操作——讀取assets和raw文件下的內(nèi)容

1.分別創(chuàng)建assets文件夾和res/raw文件夾:(要注意的raw文件是在res下new,然后創(chuàng)建一個名字為raw的文件夾)

     

2.創(chuàng)建兩個txt文件,復(fù)制到asset和raw文件夾中:

3.實現(xiàn)的效果:

4.實現(xiàn)代碼:

(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)具體實現(xiàn):

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 {
      //獲取文件中的字節(jié)
      InputStream inputStream=getResources().getAssets().open("Test.txt");
      //將字節(jié)轉(zhuǎn)換為字符
      InputStreamReader isReader=new InputStreamReader(inputStream,"UTF-8");
      //使用bufferReader去讀取內(nèi)容
      BufferedReader reader=new BufferedReader(isReader);
      String out="";
      while((out=reader.readLine())!=null){
        Log.d("讀取到的文件信息:",out);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  public void readRaw(){
    try {
      //獲取文件中的內(nèi)容
      InputStream inputStream=getResources().openRawResource(R.raw.test);
      //將文件中的字節(jié)轉(zhuǎn)換為字符
      InputStreamReader isReader=new InputStreamReader(inputStream,"UTF-8");
      //使用bufferReader去讀取字符
      BufferedReader reader=new BufferedReader(isReader);
      String out="";
      try {
        while((out=reader.readLine())!=null){
          Log.d("從raw文件夾中讀取到的數(shù)據(jù):",out);
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
  }

}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論