android之文件操作——读取assets跟raw文件下的内容

android之文件操作——读取assets和raw文件下的内容

1.分别创建assets文件夹和res/raw文件夹:(要注意的raw文件是在res下new,然后创建一个名字为raw的文件夹)

android之文件操作——读取assets跟raw文件下的内容      android之文件操作——读取assets跟raw文件下的内容

2.创建两个txt文件,复制到asset和raw文件夹中:

android之文件操作——读取assets跟raw文件下的内容

3.实现的效果:

android之文件操作——读取assets跟raw文件下的内容

android之文件操作——读取assets跟raw文件下的内容

4.实现代码:

(1)布局文件:

android之文件操作——读取assets跟raw文件下的内容android之文件操作——读取assets跟raw文件下的内容
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     tools:context="base.readassetsfile.MainActivity">
 8     <Button
 9         android:textSize="20sp"
10         android:text="@string/aasets_txt"
11         android:id="@+id/readFile"
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content" />
14     <Button
15         android:textSize="20sp"
16         android:text="@string/raw"
17         android:id="@+id/readRawFile"
18         android:layout_width="match_parent"
19         android:layout_height="wrap_content" />
20 </LinearLayout>
View Code

(2)具体实现:

android之文件操作——读取assets跟raw文件下的内容android之文件操作——读取assets跟raw文件下的内容
 1 package base.readassetsfile;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.util.Log;
 6 import android.view.View;
 7 import android.widget.EditText;
 8 
 9 import java.io.BufferedReader;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.InputStreamReader;
13 import java.io.OutputStream;
14 import java.io.UnsupportedEncodingException;
15 
16 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21         findViewById(R.id.readFile).setOnClickListener(this);
22         findViewById(R.id.readRawFile).setOnClickListener(this);
23     }
24     @Override
25     public void onClick(View v) {
26         switch (v.getId()){
27             case R.id.readFile:
28                 readAsset();
29                 break;
30             case R.id.readRawFile:
31                 readRaw();
32                 break;
33         }
34     }
35     public void readAsset(){
36         try {
37             //获取文件中的字节
38             InputStream inputStream=getResources().getAssets().open("Test.txt");
39             //将字节转换为字符
40             InputStreamReader isReader=new InputStreamReader(inputStream,"UTF-8");
41             //使用bufferReader去读取内容
42             BufferedReader reader=new BufferedReader(isReader);
43             String out="";
44             while((out=reader.readLine())!=null){
45                 Log.d("读取到的文件信息:",out);
46             }
47         } catch (IOException e) {
48             e.printStackTrace();
49         }
50     }
51     public void readRaw(){
52         try {
53             //获取文件中的内容
54             InputStream inputStream=getResources().openRawResource(R.raw.test);
55             //将文件中的字节转换为字符
56             InputStreamReader isReader=new InputStreamReader(inputStream,"UTF-8");
57             //使用bufferReader去读取字符
58             BufferedReader reader=new BufferedReader(isReader);
59             String out="";
60             try {
61                 while((out=reader.readLine())!=null){
62                     Log.d("从raw文件夹中读取到的数据:",out);
63                 }
64             } catch (IOException e) {
65                 e.printStackTrace();
66             }
67         } catch (UnsupportedEncodingException e) {
68             e.printStackTrace();
69         }
70     }
71 
72 }
View Code