Android reads content from resources

Source: Internet
Author: User

The resources in Android are read-only and not writable.

Next we read the assets directory and the Res/raw directory of text files into the TextView, the first thing to do is to put the files into these two folders

Put two TextView in activity_main.xml to display text content

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/linearlayout1"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context= "Com.ssln.fileresources.MainActivity" >    <TextViewAndroid:id= "@+id/tvraw"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"       />    <TextViewAndroid:id= "@+id/tvassets"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"        /></LinearLayout>

Then, in Mainactivity.java, 2 methods were encapsulated to read the contents of two directories, one is to get the content size, then read directly to the buffer, the other is to read the contents of the file line by row

 Packagecom.ssln.fileresources;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;Importorg.apache.http.util.EncodingUtils;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {Private FinalString Encoding = "Utf-8";//file Encoding    Private FinalString fileName = "Text.txt";//file name@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); TextView tvassets=(TextView) Findviewbyid (r.id.tvassets); TextView Tvraw=(TextView) Findviewbyid (R.id.tvraw);                Tvassets.settext (Readfromassets ());    Tvraw.settext (Readfromraw ()); }    /*** Read the files in the assets directory * *@return     */    Privatestring Readfromassets () {string Retstr= ""; Try {            //Open file to streamInputStream instream =getresources (). Getassets (). open (FileName); //Get length            intFileLen =instream.available (); //put buffer            byte[] buffer =New byte[FileLen];            Instream.read (buffer); //transcoding to TextRetstr =encodingutils.getstring (buffer, Encoding); //CloseInstream.close (); } Catch(IOException e) {e.printstacktrace (); }            returnRetstr; }    /*** Read the files in raw directory * *@return     */    Privatestring Readfromraw () {string Retstr=""; Try{InputStream instream=getresources (). Openrawresource (R.raw.text); //Here we use a different reading method to read the content line by rowBufferedReader br=NewBufferedReader (NewInputStreamReader (instream)); String TempStr="";  while((Tempstr=br.readline ())! =NULL) {Retstr+=tempstr+ "\ r \ n"; Toast.maketext ( This, TempStr, Toast.length_short). Show (); }        }        Catch(IOException e) {e.printstacktrace (); }        returnRetstr; }}

Let's see the effect.

??? Is the hair garbled? Because the code we read above is Utf-8.

Private Final // file Encoding  = encodingutils.getstring (buffer, Encoding);

Do you have a swollen handle? Let's save the file for Utf-8 with a new notepad.

Look at the effect.

Android reads content from resources

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.