Learn android< data storage from scratch (2) Internal storage internal storage. 36 .>

Source: Internet
Author: User

  1. Call with the name of the file and the openFileOutput() operating mode. This returns a FileOutputStream . by openFileOutput()建立FileoutputStream对象
  2. Write to the file with write() . Create a Write object and perform data read and write operations
  3. Close the stream with close() . Last Close link

The above is the basic steps to save the data file to internal storage

Let's take a look at the code below

XML file

<relativelayout 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:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <button android:id=" @+id/button2 "android:layout_width=" Wrap_content "Androi d:layout_height= "Wrap_content" android:layout_alignbaseline= "@+id/button1" android:layout_alignbottom= "@+id/  Button1 "android:layout_marginleft=" 51DP "android:layout_torightof=" @+id/button1 "android:text=" read Information " /> <button android:id= "@+id/button1" android:layout_width= "Wrap_content" Android:layout_hei ght= "Wrap_content" android:layout_alignparentLeft= "true" android:layout_alignparenttop= "true" android:layout_marginleft= "31DP" Android:layout_marg intop= "142DP" android:text= "Save Information"/> <textview android:id= "@+id/textview1" Android:layout_wi Dth= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparenttop= "true" Android: Layout_centerhorizontal= "true" android:layout_margintop= "47DP" android:text= "save data to internal storage" android:texta Ppearance= "? Android:attr/textappearancemedium"/></relativelayout>


Java files

Package Com.example.internalstorage;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Android.app.activity;import Android.content.Context; Import Android.os.bundle;import android.util.log;import Android.view.view;import Android.widget.button;import Android.widget.toast;public class Mainactivity extends Activity {private Button saveData, getdata;private static final St Ring FILENAME = "Flyou.txt"; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); Savedata= (Button) This.findviewbyid (R.id.button1); Getdata= (Button) This.findviewbyid (R.id.button2); Savedata.setonclicklistener (new View.onclicklistener () { Save information @overridepublic void OnClick (View v) {//TODO auto-generated method Stubfileoutputstream out = null;try {String info String = "Wind snow is not young"; out = Openfileoutput (FILENAME, context.mode_private); Out.write (Infostring.getbytes ()); Toast.Maketext (Mainactivity.this, "Data Save succeeded", 2). Show (); catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} Finally {try {if (out!=null) {Out.close ()}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace (); }}});//Read Information Getdata.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generated method Stubfileinputstream Input=null;try {input=openfileinput (FILENAME); byte[] Data=new byte[1024]; int Len=input.read (data); Input.close ();//system.out.println (new String (Data,0,len)); Toast.maketext (Mainactivity.this, New String (Data,0,len), 2). Show (); catch (FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();}});}}



Open Information



Viewing information using the file browser




Export a file


Open with Notepad



The above operation can easily implement the internal stored procedure of the file

The official API also has instructions on how to store the cache files, so you can experiment with them yourself.


The next section predicts:

External Storage External Storage

Learn android< data storage from scratch (2) Internal storage internal storage. 36 .>

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.