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