In Android, we can put some data directly in the form of a file to be present in the device. For example: Some text files, PDF files, audio and video files and pictures. Android provides a way to read and write files.
The standard Java file input stream (FileInputStream) is obtained through the Context.openfileinput () method, and the standard Java file output stream is obtained through the Context.openfileoutput () method ( FileOutputStream). Use
The Resources.openrawresource (R.raw.mydatafile) method returns InputStream.
For example, create a new activity, add two TextView and two buttons, click the first button, write the data on the TextView to a file, click the second button, and write the data in the file to TextView.
Mainactivity.java:
public class mainactivity extends activity { private string filename = "file.txt"; private textview mytext1,mytext2 ; @Override protected void oncreate (Bundle savedinstancestate) { super.oncreate (savedInstanceState); setcontentview (R.layout.activity_main); mytext1= (TextView) This.findviewbyid (R.ID.TEXT1); mytext2= (TextView) This.findviewbyid (R.ID.TEXT2); button button1= (Button) This.findviewbyid (r.id.btn_read); button button2= (Button) This.findviewbyid (r.id.btn_write); button1.setonclicklisteneR (New onclicklistener () { @Override public void onclick (View  ARG0) { // TODO Auto-generated method stub mytext2.settext (Read ()); } }); button2.setonclicklistener (New onclicklistener () { @Override public void onclick (view arg0) { // todo auto-generated method stub write (Mytext1.getText (). toString ()); } }); } protected string read () { try { fileinputstream fis = openfileinput (filename); try { byte[] buffer = new byte[fis.available ()]; fis.read (buffer); return new string (buffer); } catch (Ioexception e) { // TODO Auto-generated catch block e.printstacktrace (); } } catch ( Filenotfoundexception e) { // TODO Auto-generated catch block e.printstacktrace (); } return null;&nbSp; } protected void write (STRING STR) { try { fileoutputstream fos = openfileoutput (filename,mode_append); try { fos.write (Str.getbytes ()); fos.close (); } catch (ioexception e) { // TODO auto-generated catch block e.printstacktrace (); } } catch (filenotfoundexception e) { // TODO Auto-generated catch block e.printstacktrace (); } }}
Activity_main.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http// Schemas.android.com/apk/res/android " android:layout_width=" Fill_parent " android:layout_height= "fill_parent" android:orientation= "vertical" > <textview android:id= "@+id/text1" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= " I am Text1 "/> <button android:id=" @+id/btn_write " android:layout_width= "Match_parent" android: layout_height= "Wrap_content" android:text= "Write"/> <textview android:id="@+id/text2" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= "I am Text2"/> <button android:id= "@+id/btn_read" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= "read"/> </linearlayout>
The results of the operation are as follows: Click Write to write the first textview to the file, click Read to readout the data to the second TextView
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/5A/D7/wKiom1T9pIajsG7cAADJWMCyuJo675.jpg "title=" Qq20150309214359.png "alt=" wkiom1t9piajsg7caadjwmcyujo675.jpg "/> 650" This.widt h=650; "src=" http://s3.51cto.com/wyfs02/M01/5A/D2/wKioL1T9pavjCvM6AADC0C0wbRA526.jpg "title=" 2.png "alt=" Wkiol1t9pavjcvm6aadc0c0wbra526.jpg "/>
This article is from the "Useless Uncle" blog, please be sure to keep this source http://aslonely.blog.51cto.com/6552465/1618819
Android data Storage (ii) Use of files