All applications on Android devices have a directory of files placed in the sandbox. Saving the files in the sandbox can prevent other applications from accessing them.
The full path of the sandbox directory is/data/<package name> viewed in file Explorer:
As you can see, each application has a file directory named after this package under/data.
This article describes how to save files in the/data/<package name>/files/directory.
The following shows how to store and load local files on an internal storage device:
1. Create a project named datastorage
2. Prepare the layout file (activity_data_storage.xml)
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Orientation = "vertical"> <textview Android: id = "@ + ID/data_view" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "not operated"/> <button Android: id = "@ + ID/save_button" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: text = "save data"/> <button Android: id = "@ + ID/load_button" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: text = "loading data"/> </linearlayout>
3. datastorageactivity. Java
Package COM. example. datastorage; import Java. io. bufferedreader; import Java. io. filenotfoundexception; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. outputstream; import Java. io. outputstreamwriter; import Java. io. writer; import android. content. context; import android. OS. bundle; import android. support. v7.app. actionbaractivity; import android. util. log; imp ORT android. view. view; import android. widget. button; import android. widget. textview; import android. widget. toast; public class datastorageactivity extends actionbaractivity {Private Static final string filename = "data.txt"; Private Static final string tag = "datastorageactivity"; private textview dataview; private button savebutton; private button loadbutton; @ overrideprotected void oncreate (bundle savedi Nstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_date_storage); dataview = (textview) findviewbyid (R. id. data_view); savebutton = (button) findviewbyid (R. id. save_button); loadbutton = (button) findviewbyid (R. id. load_button); setlistener ();} private void setlistener () {savebutton. setonclicklistener (new view. onclicklistener () {public void onclick (view v) {try {savedata ();} Catch (ioexception e) {} toast. maketext (datastorageactivity. this, "saved successfully", toast. length_short ). show () ;}}); loadbutton. setonclicklistener (new view. onclicklistener () {public void onclick (view v) {try {loaddata ();} catch (filenotfoundexception e) {} catch (ioexception e ){}}});} public void savedata () throws ioexception {outputstream out = This. openfileoutput (filename, context. mode_private);/* parameter 1: file name. * If the file does not exist, Android automatically creates it. The created file is saved in the/data/<package name>/Files directory * parameter 2: file operation mode parameters. Indicates that the file is private data and can only be accessed by the application itself. **/Writer = new outputstreamwriter (out); try {string STR = "data from internal storage devices"; writer. write (STR);} finally {writer. close () ;}} public void loaddata () throws filenotfoundexception, ioexception {bufferedreader reader = NULL; stringbuilder DATA = new stringbuilder (); try {inputstream in = This. openfileinput (filename); log. I (TAG, in. tostring (); reader = new bufferedreader (New inputstreamreader (in); Str Ing line = new string (); While (line = reader. Readline ())! = NULL) {data. append (line);} dataview. settext (data);} catch (filenotfoundexception e) {dataview. settext ("No stored data found");} finally {reader. close ();}}}
Run the program and click "save data". After the toast is successfully saved. Click the load data button again:
It can be found that the device stored in the internal storage device is loaded and displayed in textview. Check the file location again:
# Done #
Store and load local files (internal storage devices)