Detailed complete example of Android file storage

Source: Internet
Author: User

Mainactivity as follows:

Package Cc.cv;import Java.io.bufferedreader;import Java.io.bufferedwriter;import java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.inputstreamreader;import Java.io.outputstreamwriter;import Android.os.bundle;import Android.app.activity;import Android.content.context;import Android.view.View;import Android.view.view.onclicklistener;import android.widget.button;/** * Demo Description: * File Store Detailed example * * Available with context's openfileoutput ( ) method to store the data in a file. * The default storage path is/data/data/<packagename>/files/* You can use the context's Openfileinput () method to read the data from a file * * The storage method commonly used in Android: * sharedpreferences, database, SD card, file storage. * For the last way, usually not much, now write an example * collation and review.    * Note: * File storage is not required permissions and file storage on the SD card is not the same */public class Mainactivity extends Activity {private Button Msavebutton;    Private Button Mgetbutton;    Private Context Mcontext; Private String mfilename; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.main); init ();} private void Init () {McontExt=this;mfilename= "TestData"; msavebutton= (Button) Findviewbyid (R.id.savebutton); Msavebutton.setonclicklistener (New Clicklistenerimpl ()); mgetbutton= (Button) Findviewbyid (R.id.getbutton); Mgetbutton.setonclicklistener (new Clicklistenerimpl ());} Private class Clicklistenerimpl implements onclicklistener{@Overridepublic void OnClick (view view) {switch (View.getid ( ) {case R.id.savebutton:savedatatofile (mcontext,mfilename); Break;case R.id.getbutton:getdatafromfile (MContext, Mfilename); break;default:break;}}} /** * Save data to file */private void Savedatatofile (Context context,string fileName) {String data= "Hello,all"; FileOutputStream Fileoutputstream=null;outputstreamwriter Outputstreamwriter=null; BufferedWriter bufferedwriter=null;try {fileoutputstream=context.openfileoutput (fileName, Context.MODE_PRIVATE); o Utputstreamwriter=new OutputStreamWriter (fileoutputstream); bufferedwriter=new BufferedWriter (OutputStreamWriter) ; bufferedwriter.write (data);} catch (Exception e) {e.printstacktrace ();} Finally{try {if (bufferedwriter!=null) {bufferedwriter.close ();}} catch (Exception e) {e.printstacktrace ()}}} /** * Read data from file */private void Getdatafromfile (Context context,string fileName) {FileInputStream fileinputstream=null ; InputStreamReader Inputstreamreader=null; BufferedReader Bufferedreader=null; StringBuilder Stringbuilder=null; String line=null;try {stringbuilder=new stringBuilder (); Fileinputstream=context.openfileinput (FileName); Inputstreamreader=new InputStreamReader (FileInputStream); bufferedreader=new BufferedReader (InputStreamReader); while ((Line=bufferedreader.readline ())!=null) {stringbuilder.append (line);} SYSTEM.OUT.PRINTLN ("--->" +stringbuilder.tostring ());} catch (Exception e) {e.printstacktrace ();} Finally{try {if (bufferedreader!=null) {bufferedreader.close ();}} catch (Exception e) {e.printstacktrace ();}}}}

Main.xml as follows:
<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 "    >    <button        android:id= "@+id/savebutton"        android:layout_width= "Wrap_content"        android: layout_height= "Wrap_content"        android:text= "save data to File"        android:layout_centerhorizontal= "true"        android:layout_margintop= "100DP"/>             <button        android:id= "@+id/getbutton"        android:layout_ Width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "get data from File"        android: Layout_centerhorizontal= "true"        android:layout_margintop= "260DP"/></relativelayout>


Android File Store detailed complete example

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.