Android file operation-read assets and RAW file content
1. Create Assets folders and Res/raw folders individually: (Note that raw files are new in res and then create a folder with the name Raw)
2. Create two txt files and copy to asset and raw folders:
3. Effect of implementation:
4. Implementation code:
(1) Layout file:
<?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) Specific implementation:
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 oncreat
E (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 The public void Readasset () {try {//Get file inbyte InputStream inputstream=getresources (). Getassets (). Open ("Test.txt");
Converts bytes to characters inputstreamreader isreader=new InputStreamReader (InputStream, "UTF-8");
Use Bufferreader to read content BufferedReader reader=new BufferedReader (Isreader);
String out= "";
while ((Out=reader.readline ())!=null) {log.d ("File information read:", out);
} catch (IOException e) {e.printstacktrace (); } public void Readraw () {try {//Get the contents of the file InputStream inputstream=getresources (). Openrawresource (R.R
Aw.test);
Converts the bytes in the file to characters InputStreamReader isreader=new InputStreamReader (InputStream, "UTF-8");
Use Bufferreader to read characters BufferedReader reader=new BufferedReader (Isreader);
String out= "";
try {while (Out=reader.readline ())!=null) {LOG.D ("data read from Raw folder:", out);
} catch (IOException e) {e.printstacktrace (); } catch (Unsupportedencodingexception e) {e.printstAcktrace (); }
}
}
Thank you for reading, I hope to help you, thank you for your support for this site!