Here are five ways to introduce the Android data store:
1, Sharedpreferences
2. File storage
3. SQLite database
4, ContentProvider
5. Networked Storage
This article mainly describes how to use files to store data. The Android file operation uses the FileOutputStream and FileInputStream classes in the java.io.
First, storage files
First instantiate a fileoutputstream.
FileOutputStream Fostream = Openfileoutput (FileName, mode_private);
FileName: The name to write to the file
Mode_private: For the default mode of operation, the file is private data and can only be accessed by the application itself, in which case the written content overwrites the contents of the original file.
Mode_append: Mode checks whether the file exists, appends to the file, or creates a new file.
Mode_world_readable: Indicates that the current file can be read by another application and is not recommended
Mode_world_writeable: Indicates that the current file can be written to another application and is not recommended for use
Then call Fostream.write () to complete the write.
byte[] buffer = filecontent.getbytes ();
Fostream.write (buffer);
Toast.maketext (Mainactivity.this, "write Success", Toast.length_short). Show ();
Finally, do some cleanup work, refresh the write flow and close the stream.
Fostream.flush ();
Fostream.close ();
Second, read the file
Again, first instantiate a fileinputstream.
FileInputStream Fistream = openfileinput (fileName)
Then call Fistream.read ()
int len = fistream.available ();
byte[] buffer = new Byte[len];
Fistream.read (buffer)
Finally, display the text and close the read file stream
Etcontent.settext (new String (buffer));
Toast.maketext (Mainactivity.this, "read successful", Toast.length_short). Show ();
Fistream.close ();
Three, complete code
Import android.support.v.app.appcompatactivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.Toast;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
public class Mainactivity extends Appcompatactivity {private EditText etname;
Private EditText etcontent;
Private Button Btnwrite;
Private Button Btnread;
Private String FileName = "";
Private String filecontent = "";
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Etname = (edittext) Findviewbyid (r.id.etname);
Etcontent = (edittext) Findviewbyid (r.id.etcontent);
Btnwrite = (Button) Findviewbyid (r.id.btnwrite);
Btnread = (Button) Findviewbyid (R.id.btnread);
Btnwrite.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) { Filename = Etname.gettext (). toString ();
Filecontent = Etcontent.gettext (). toString ();
try {fileoutputstream fostream = Openfileoutput (FileName, mode_private);
byte[] buffer = filecontent.getbytes ();
Fostream.write (buffer);
Toast.maketext (Mainactivity.this, "write Success", Toast.length_short). Show ();
Fostream.flush ();
Fostream.close ();
}catch (Exception e) {e.printstacktrace ();
}
}
});
Btnread.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {
FileName = Etname.gettext (). toString ();
try{FileInputStream Fistream = Openfileinput (fileName);
int len = fistream.available ();
byte[] buffer = new Byte[len];
Fistream.read (buffer);
Etcontent.settext (new String (buffer));
Toast.maketext (Mainactivity.this, "read successful", Toast.length_short). Show (); Fistream.close ();
}catch (Exception e) {e.printstacktrace ();
}
}
}); } <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: paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" Android :p addingtop= "@dimen/activity_vertical_margin" android:paddingbottom= "@dimen/activity_vertical_margin" tools: Context= ".
Mainactivity "> <edittext android:layout_width=" wrap_content "android:layout_height=" Wrap_content " Android:id= "@+id/etname" android:layout_alignparenttop= "true" android:layout_alignparentleft= "true" Android
: Layout_alignparentstart= "True" android:layout_alignparentright= "true" android:layout_alignparentend= "true" android:text= "filename"/> <edittext android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:id= "@+id/etcontent" Android:layout_below = "@+id/etname" android:layout_alignparentleft= "true" android:layout_alignparentstart= "true" Android:layout_ Alignparentright= "true" android:layout_alignparentend= "true" android:text= "file contents"/> <button Androi D:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Save" android:id= "@+id/btnWr Ite "android:layout_aligntop=" @+id/btnread "android:layout_toleftof=" @+id/btnread "Android:layout_tostartof"
= "@+id/btnread"/> <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "read" android:id= "@+id/btnread" android:layout_below= "@+id/etcontent" Android:layout_alignpar Entright= "true" android:layout_alignparentend= "true"/> </RelativeLayout>