android之檔案操作——讀取assets和raw檔案下的內容

來源:互聯網
上載者:User

標籤:closed   ada   idt   splay   support   tools   activity   read   擷取   

1.分別建立assets檔案夾和res/raw檔案夾:(要注意的raw檔案是在res下new,然後建立一個名字為raw的檔案夾)

      

2.建立兩個txt檔案,複製到asset和raw檔案夾中:

3.實現的效果:

4.實現代碼:

(1)布局檔案:

 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     <Button15         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)具體實現:

 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     @Override18     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     @Override25     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

 

android之檔案操作——讀取assets和raw檔案下的內容

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.