Android 讀取assets和raw文件內(nèi)容實(shí)例代碼
android之文件操作——讀取assets和raw文件下的內(nèi)容
1.分別創(chuàng)建assets文件夾和res/raw文件夾:(要注意的raw文件是在res下new,然后創(chuàng)建一個(gè)名字為raw的文件夾)
2.創(chuàng)建兩個(gè)txt文件,復(fù)制到asset和raw文件夾中:
3.實(shí)現(xiàn)的效果:
4.實(shí)現(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)具體實(shí)現(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(); } } }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- 讀寫Android中assets目錄下的文件的方法詳解
- Android獲取assets文件夾中的數(shù)據(jù)并寫入SD卡示例
- android讀取assets文件示例
- 基于android中讀取assets目錄下a.txt文件并進(jìn)行解析的深入分析
- Android操作存放在assets文件夾下SQLite數(shù)據(jù)庫的方法
- Android播放assets文件里視頻文件相關(guān)問題分析
- Android編程讀取Assets所有文件(遍歷每一個(gè)文件夾)并存入sdcard的方法
- android文件操作——讀取assets和raw文件下的內(nèi)容
- Android 加載assets中的資源文件實(shí)例代碼
- Android 中raw和assets文件夾的區(qū)別
- Android API編程之Assets文件操作示例
- Android開發(fā)實(shí)現(xiàn)讀取Assets下文件及文件寫入存儲(chǔ)卡的方法
相關(guān)文章
Android自定義recyclerView實(shí)現(xiàn)時(shí)光軸效果
這篇文章主要介紹了Android自定義recyclerView實(shí)現(xiàn)時(shí)光軸效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Android xml實(shí)現(xiàn)animation的4種動(dòng)畫效果實(shí)例代碼
在Android應(yīng)用程序,使用動(dòng)畫效果,能帶給用戶更好的感覺,做動(dòng)畫可以通過XML或Android代碼來實(shí)現(xiàn)。本文給大家介紹Android xml實(shí)現(xiàn)animation的4種動(dòng)畫效果實(shí)例代碼,一起看看吧2016-05-05Android消息推送:手把手教你集成小米推送(附demo)
本篇文章主要介紹了Android消息推送:手把手教你集成小米推送,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-12-12Android入門之在子線程中調(diào)用Handler詳解
這篇文章主要為大家詳細(xì)介紹了Android如何在子線程中調(diào)用Handler,文中的示例代碼講解詳細(xì),有需要的朋友可以借鑒參考下,希望能夠?qū)Υ蠹矣兴鶐椭?/div> 2022-12-12Android實(shí)現(xiàn)調(diào)用手機(jī)攝像頭錄像限制錄像時(shí)長(zhǎng)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)調(diào)用手機(jī)攝像頭錄像限制錄像時(shí)長(zhǎng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Android AutoCompleteTextView控件基本用法示例
這篇文章主要介紹了Android AutoCompleteTextView控件基本用法,結(jié)合實(shí)例形式分析了AutoCompleteTextView控件的功能、使用方法及相關(guān)操作步驟,需要的朋友可以參考下2016-06-06Kotlin StateFlow單數(shù)據(jù)更新熱流設(shè)計(jì)與使用介紹
StateFlow當(dāng)值發(fā)生變化,就會(huì)將值發(fā)送出去,下流就可以接收到新值。在某些場(chǎng)景下,StateFlow比LiveData更適用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09Android sqlite cursor的遍歷實(shí)例詳解
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于Android sqlite cursor的遍歷的相關(guān)實(shí)例及知識(shí)點(diǎn),需要的朋友們可以學(xué)習(xí)下。2021-06-06Android調(diào)用系統(tǒng)時(shí)間格式顯示時(shí)間信息
這篇文章主要介紹了Android調(diào)用系統(tǒng)時(shí)間格式顯示時(shí)間信息的使用方法,代碼很簡(jiǎn)單2014-01-01最新評(píng)論