Android reads assets and raw file content instance code _android

Source: Internet
Author: User
Tags readfile readline

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.