Android file operation-read the contents of assets and raw files

Source: Internet
Author: User
Tags readfile

1. Create the Assets folder and the Res/raw folder separately: (Note that the raw file is new under Res, and then create a folder with the name Raw)

2. Create two txt files and copy them to the asset and raw folders:

3. The effect of the implementation:

4. Implementation code:

(1) Layout file:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns: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     <Button9         android:textsize= "20SP"Ten Android:text= "@string/aasets_txt" One Android:id= "@+id/readfile" A Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" /> -     <Button the         android:textsize= "20SP" - Android:text= "@string/raw" - Android:id= "@+id/readrawfile" - Android:layout_width= "Match_parent" + Android:layout_height= "Wrap_content" /> - </LinearLayout>
View Code

(2) Concrete realization:

1  PackageBase.readassetsfile;2 3 Importandroid.support.v7.app.AppCompatActivity;4 ImportAndroid.os.Bundle;5 ImportAndroid.util.Log;6 ImportAndroid.view.View;7 ImportAndroid.widget.EditText;8 9 ImportJava.io.BufferedReader;Ten Importjava.io.IOException; One ImportJava.io.InputStream; A ImportJava.io.InputStreamReader; - ImportJava.io.OutputStream; - Importjava.io.UnsupportedEncodingException; the  -  Public classMainactivityextendsAppcompatactivityImplementsView.onclicklistener { - @Override -     protected voidonCreate (Bundle savedinstancestate) { +         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); +Findviewbyid (R.id.readfile). Setonclicklistener ( This); AFindviewbyid (R.id.readrawfile). Setonclicklistener ( This); at     } - @Override -      Public voidOnClick (View v) { -         Switch(V.getid ()) { -              CaseR.id.readfile: - Readasset (); in                  Break; -              CaseR.id.readrawfile: to Readraw (); +                  Break; -         } the     } *      Public voidReadasset () { $         Try {Panax Notoginseng             //get bytes in a file -InputStream inputstream=getresources (). Getassets (). Open ("Test.txt"); the             //Convert bytes to characters +InputStreamReader isreader=NewInputStreamReader (InputStream, "UTF-8"); A             //use Bufferreader to read content theBufferedReader reader=NewBufferedReader (isreader); +String out= ""; -              while((Out=reader.readline ())! =NULL){ $LOG.D ("Read file information:", out); $             } -}Catch(IOException e) { - e.printstacktrace (); the         } -     }Wuyi      Public voidReadraw () { the         Try { -             //get the contents of a file WuInputStream inputstream=getresources (). Openrawresource (r.raw.test); -             //converts a byte in a file to a character AboutInputStreamReader isreader=NewInputStreamReader (InputStream, "UTF-8"); $             //use Bufferreader to read characters -BufferedReader reader=NewBufferedReader (isreader); -String out= ""; -             Try { A                  while((Out=reader.readline ())! =NULL){ +LOG.D ("Data read from Raw folder:", out); the                 } -}Catch(IOException e) { $ e.printstacktrace (); the             } the}Catch(unsupportedencodingexception e) { the e.printstacktrace (); the         } -     } in  the}
View Code

Android file operation-read the contents of assets and raw files

Related Article

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.