The files of the operation in Android can be divided into: file, XML, Sharedpreference.
This blog mainly describes the operation of file:
1, Mainactivity
Package Com.example.filetest;import Android.os.bundle;import Android.os.environment;import android.app.Activity; Import Android.view.menu;import android.view.view;import android.widget.button;import android.widget.EditText; Import Android.widget.toast;public class Mainactivity extends Activity {private EditText et_name;private EditText et_ Content;private Button sdbtn; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.main); et_name = (EditText) Findviewbyid (r.id.nameet); et_content = ( EditText) Findviewbyid (r.id.contentet); sdbtn = (Button) Findviewbyid (R.ID.SDCBT);} @Overrideprotected void Onresume () {///when the interface switches to the foreground is performed this method ... Super.onresume ();//depending on the status of the SD card the button is available if (! Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {sdbtn.setenabled (false);} Else{sdbtn.setenabled (True);}} public void OnClick (view view) {String name = Et_name.gettext (). toString (); String content = Et_content.gettext (). toString (); Fileservice ServiCE = new Fileservice (this), Try{switch (View.getid ()) {case r.id.rombt:system.out.println ("---------> Save to Rom"); Service.savetorom (name,content) break;case r.id.sdcbt:system.out.println ("---------> Save to SD card"); Service.savetosd (name,content); break;} Toast.maketext (Getapplicationcontext (), "Save succeeded", 1). Show (); catch (Exception e) {e.printstacktrace (); Toast.maketext (Getapplicationcontext (), "Save Failed", 1). Show ();}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
2, Fileservice
Package Com.example.filetest;import Java.io.fileoutputstream;import Android.content.context;import Android.os.environment;public class Fileservice {private Context context;public Fileservice (context context) { This.context = context;} public void Savetosd (string name, string content) throws exception{/** * Create output stream specify where the SD card is located * environment.getexternalstorage Directory (): Gets the location of the SD card for/mnt/sdcard * */fileoutputstream fos = new FileOutputStream ( Environment.getexternalstoragedirectory () + "/" + name); Fos.write (Content.getbytes ()); Fos.close ();} public void Savetorom (string name, string content) throws exception{/** * If open successfully, will be created in the/data/data/package name/Under Love you a Files directory, And put the files in there * * Context.mode_private: Only you can visit the files created * Context.mode_world_readable: The world can read, that is, anyone can read * Context.mode_worl D_writeable: The world can write, that is to say, anyone can write. * context.mode_world_readable + context.mode_world_writeable: Read and write all over the world ... * context.mode_append: Private && can only be added * * Created when set to Context.mode_private, at this time, other programs want to read and want to write can not, can only be his own read and write ... */fileoutputsTream fos = context.openfileoutput (name, context.mode_world_readable);//Open the/data/data/app for a file Fos.write ( Content.getbytes ());//write content to ROM without permission fos.close ();}}
3. Filetest (for testing)
Package Com.example.filetest;import Java.io.bytearrayoutputstream;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Android.content.context;import Android.test.androidtestcase;public class FileTest Extends Androidtestcase {/** * Read (normal result: can be successful). Used to test whether the Context.mode_private function correctly * @throws Exception */public void Testread () throws Exception{fileinputstream fis = GetContext (). Openfileinput ("Wr.txt");//fileinputstream in = new FileInputStream ("/data/data/com.exmaple.filetest/files/hjd.txt");//This notation is equivalent to the one written above bytearrayoutputstream BAOs = New Bytearrayoutputstream (); byte[] buffer = new Byte[1024];int len;while (len = fis.read (buffer))! =-1) {Baos.write ( Buffer,0,len);} Fis.close (); Baos.close (); String content = new String (Baos.tobytearray ()); SYSTEM.OUT.PRINTLN ("---->content:" + content);} public void Testwrite () throws Exception{fileoutputstream fos = new FileOutputStream ("/data/data/com.example.filetest/ Files/hjd.txt "); String content = "Hahahahah"; Fos.write (Content.getbytes ()); Fos.close ();}}
Two
Several modes of the context can actually be seen from the/data/data/application name/files/file in Ddms.
SOURCE Download: http://download.csdn.net/detail/caihongshijie6/7613405