Android:讀取Assets檔案和raw檔案
阿新 • • 發佈:2019-02-15
一、讀取Assets檔案
Assets檔案不能用R.xxx.XXX呼叫
1.新建assets目錄,並在目錄中建一個txt檔案
新建assets
新建txt檔案
2.讀取該檔案資料
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="zs.readassetsdemo.MainActivity"> <Button android:id="@+id/read_txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="讀取txt"/> </LinearLayout>
MainActivity.java
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class MainActivity extends AppCompatActivity { private static final String TAG = "ReadAssets"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.read_txt).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { //通過輸入流獲取檔案資訊 InputStream is = getResources().getAssets().open("test.txt"); //把輸入流轉換為字元流 InputStreamReader isr = new InputStreamReader(is,"utf-8"); //把字元流包裝為BufferReader BufferedReader bfr = new BufferedReader(isr); String in = ""; while ((in = bfr.readLine()) != null) { Log.i(TAG,in); } } catch (IOException e) { e.printStackTrace(); } } }); } }
效果如下:
點選讀取txt
二、讀取raw檔案
1.在res目錄下新建一個raw資源目錄
2.在raw目錄下建一個txt檔案
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="zs.readrawdemo.MainActivity"> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="讀取txt"> </Button> </android.support.constraint.ConstraintLayout>
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
//獲取raw目錄下的檔案可以直接用R.raw.xxx
InputStream is = getResources().openRawResource(R.raw.test);
InputStreamReader isr = new InputStreamReader(is,"utf-8");
BufferedReader br = new BufferedReader(isr);
String in = "";
while ((in = br.readLine())!= null) {
System.out.println(in);
}
}catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
效果如下:
點選讀取TXT