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();
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
- 讀寫Android中assets目錄下的文件的方法詳解
- Android獲取assets文件夾中的數(shù)據(jù)并寫入SD卡示例
- android讀取assets文件示例
- 基于android中讀取assets目錄下a.txt文件并進行解析的深入分析
- Android操作存放在assets文件夾下SQLite數(shù)據(jù)庫的方法
- Android播放assets文件里視頻文件相關(guān)問題分析
- Android編程讀取Assets所有文件(遍歷每一個文件夾)并存入sdcard的方法
- android文件操作——讀取assets和raw文件下的內(nèi)容
- Android 加載assets中的資源文件實例代碼
- Android 中raw和assets文件夾的區(qū)別
- Android API編程之Assets文件操作示例
- Android開發(fā)實現(xiàn)讀取Assets下文件及文件寫入存儲卡的方法
相關(guān)文章
Android用PopupWindow實現(xiàn)自定義Dailog
這篇文章主要為大家詳細介紹了Android用PopupWindow實現(xiàn)自定義Dailog的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
Android WebViewClient 的 `shouldOverrideUrlLoa
這篇文章主要介紹了Android WebViewClient 的 shouldOverrideUrlLoading方法,了解并正確實現(xiàn) WebViewClient 中的 shouldOverrideUrlLoading 方法對于在你的 Android 應(yīng)用中提供順暢且安全的瀏覽體驗至關(guān)重要,需要的朋友可以參考下2024-07-07
Android Studio打包APK文件具體實現(xiàn)步驟解析
這篇文章主要介紹了Android Studio打包APK文件具體實現(xiàn)步驟解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11

