Android learning notes: internal storage of data; practice Data Reading and writing; storage to Cache for reading and writing; android learning notes
(1) directory structure
(2) Layout 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: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <EditText android: id =" @ + id/editText1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_alignParentLeft =" true "android: layout_alignParentTop = "true" android: layout_marginLeft = "28dp" android: layout_marginTop = "17dp" android: EMS = "10"/> <Button android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignLeft = "@ + id/editText1" android: layout_below = "@ + id/editText1" android: layout_marginTop = "54dp" android: text = "save information"/> </RelativeLayout>
(3) tool class for saving and reading data: FileService. java
Package com. example. data_storage_interal.file; import java. io. byteArrayOutputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import android. content. context;/*** internal storage is used to clear all related information when the application is uninstalled ** @ author piaodangdehun **/public class FileService {private Context context; public FileService (Conte Xt context) {this. context = context;}/*** Save the content to the file ** @ param fileName *: file name * @ param mode *: mode * @ param data *: data Buffer * @ return returns the true/false value */public boolean saveContentToFile (String fileName, int mode, byte [] data) {boolean flag = false; FileOutputStream outputStream = null; try {outputStream = context. openFileOutput (fileName, mode); try {outputStream. write (data, 0, data. length);} catch (IOException E) {e. printStackTrace ();} flag = true;} catch (FileNotFoundException e) {e. printStackTrace ();} finally {if (outputStream! = Null) {try {outputStream. close () ;}catch (Exception e2) {}} return true ;} /*** read data ** @ param fileName * @ return */public String readContentFromFile (String fileName) {String result = ""; FileInputStream fileInputStream = null; byteArrayOutputStream outputStream = new ByteArrayOutputStream (); try {fileInputStream = context. openFileInput (fileName); int len = 0; byte [] data = new byte [1024]; while (len = FileInputStream. read (data ))! =-1) {outputStream. write (data, 0, len);} return new String (outputStream. toByteArray ();} catch (Exception e) {e. printStackTrace ();} return "";}/** save the file to Chace */public boolean saveCacheFile (String fileName, byte [] data) {boolean flag = false; file file = context. getFilesDir (); FileOutputStream outputStream = null; try {File folderFile = new File (file. getAbsoluteFile () + "/txt"); if (! FolderFile. exists () {folderFile. mkdirs (); // create directory} // context. openFileOutput ("my.txt", context. MODE_WORLD_READABLE); outputStream = new FileOutputStream (folderFile. getAbsolutePath () + "/" + fileName); try {outputStream. write (data, 0, data. length);} catch (IOException e) {e. printStackTrace () ;}} catch (FileNotFoundException e) {e. printStackTrace ();} finally {if (outputStream! = Null) {try {outputStream. close ();} catch (IOException e) {e. printStackTrace () ;}}// System. out. println ("-->" + file. getAbsolutePath (); return flag;}/** obtain the complete file path */public void listCacheFile () {/** String [] strings = context. fileList (); for (int I = 0; I <* strings. length; I ++) {System. out. println ("-->" + strings);} */File file = context. getFilesDir (); File root = new File (file. getAbsoluteFile () + "/txt"); File [] listFiles = root. listFiles (); for (File file2: listFiles) {System. err. println ("--- >>>" + file2.getAbsolutePath ());}}}
(4) MainActivity. java
Package com. example. data_storage_interal; import com. example. data_storage_interal.file.FileService; import android. OS. bundle; import android. app. activity; import android. content. context; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; import android. widget. toast; public class MainActivity extends Activi Ty {private Button button; private EditText editText; private FileService fileService; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) this. findViewById (R. id. button1); editText = (EditText) this. findViewById (R. id. editText1); fileService = new FileService (this);/** save it to the file when the button is clicked */button. setOnClickL Istener (new OnClickListener () {@ Overridepublic void onClick (View v) {String valueString = editText. getText (). toString (). trim (); boolean flag = fileService. saveContentToFile ("bb.txt", Context. MODE_APPEND, valueString. getBytes (); if (flag) {Toast. makeText (MainActivity. this, "saved successfully! ", Toast. LENGTH_SHORT ). show () ;}}) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
(5) test class (add the corresponding information to the resource file, not to mention here)
Package com. example. data_storage_interal; import com. example. data_storage_interal.file.FileService; import android. app. service; import android. content. context; import android. test. androidTestCase; import android. util. log; public class MyTest extends AndroidTestCase {private static final String TAG = "MyTest";/** save data */public void save () {FileService fileService = new FileService (getContext (); boolean f Lag = fileService. saveContentToFile ("aa.txt", Context. MODE_PRIVATE + Context. MODE_APPEND, "nihao ". getBytes (); Log. I (TAG, "--->" + flag);}/** save data */public void read () {FileService fileService = new FileService (getContext ()); string msg = fileService. readContentFromFile ("bb.txt"); Log. I (TAG, "--->" + msg);} public void testChaceFile () {FileService fileService = new FileService (getContext (); fileServ Ice. saveCacheFile ("hello.txt", "hello world! ". GetBytes ();} public void testListFile () {FileService fileService = new FileService (getContext (); fileService. listCacheFile ();}}
After the file is saved successfully, the corresponding file is found in the data-apk installation directory: