1. Save the file in the device's internal private storage, other applications cannot access;
2. After the application is uninstalled, the file is automatically deleted.
3. Caching
-----------------------------
----------------------------Test---------------------------
-------------------------------------
-----------------Test---------------------
------------------input, deposit file------------------------------
Requires EditText and button
----------------------------------saved to the cache file---------------------------------------------------
1 //----------------saved to the buffer file----------------------2 Public BooleanSavecachefile (String filename,byte[]data] {3 BooleanFlag =false;4File File =Context.getfilesdir ();5FileOutputStream OutputStream =NULL;6 Try {7File folderfile=NewFile (File.getabsolutepath () + "/txt");//create file txt directory8 if(!folderfile.exists ()) {9Folderfile.mkdirs ();//Create a directoryTen } One //outputstream = context.openfileoutput ("My.txt", A //context. mode_world_writeable); -outputstream=NewFileOutputStream (Folderfile.getabsolutepath () + "/" +filename); -Outputstream.write (data,0, data.length); the}Catch(Exception e) { - //Todo:handle Exception - e.printstacktrace (); -}finally { + if(OutputStream! =NULL) { - Try { + outputstream.close (); A}Catch(Exception E2) { at //Todo:handle Exception - } - - } - - } in //System.out.println ("---->>" +file.getabsolutepath ()); - //-->>/data/data/com.example.android_datastorage_internal/files to + returnFlag; -}
---------------------------
1 //-------------Traverse Content-------------------2 Public voidListcachefile () {3 4 //string[] Strings =context.filelist ();5 //For (String string:strings) {6 //System.out.println ("----->>" +string);//list folders: txt7 // }8 //traversing Files9File file=Context.getfilesdir ();TenFile root=NewFile (File.getabsolutepath () + "/txt"); OneFile[] Listfile=root.listfiles (); A for(File file2:listfile) { -SYSTEM.OUT.PRINTLN ("---->>" +file2.getname ());//list the files inside the folder Hello.txt - the } - } -}
------Test----------------------------------------
1 Public void Test () {2 Fileservice service=New Fileservice (GetContext ()); 3 // service.savecachefile ("Hello.txt", "Hello". GetBytes ()); 4 service.listcachefile (); 5 6 }
---------------------------
Android starts with internal storage