source Download (download points free): Download
You can store the data directly into the internal storage, and by default, the file is stored in the internal storage that is private and cannot be
Other programs are accessed, and when the application is uninstalled, the files are removed.
There are two ways to create and write data:
- Using the related methods in Java,
- Using the relevant methods in Android.content,
- Call Openfileoutput () and return the FileOutputStream object
- Call the Write () method of the FileOutputStream object
- Close the stream
Reading a file is basically the same way.
There is a little trick in reading a file: If you want to save a static file in your application at compile time, save the file to
Res/raw/directory. You can use the Openrawresource () method to open the file and return a InputStream
object, and then you can read and write data.
Code:
Method One:
FileOutputStreamWritestream= NULL;FileOutputStreamFileOutputStream= NULL;Switch (View.getId()) { CaseR.ID.button1: //Created files other programs cannot access Filefile1= New File(Getfilesdir(),FILE1); Try { //Write DataWritestream= New FileOutputStream(file1);Writestream.Write("haha".getBytes()); } Catch (FileNotFoundExceptione) {e.Printstacktrace(); } Catch (IOExceptione) {e.Printstacktrace(); } finally{ Try { if (Writestream!= NULL)Writestream.Close(); } Catch (IOExceptione) {e.Printstacktrace(); }}
Method Two:
//Using the Android.content,Try { /* Set to Mode_private, other programs can not access, this is the default way of construction, you can set to other ways to make it readable and writable. */FileOutputStream=Openfileoutput(FILE2,Context.mode_private); //Write DataFileOutputStream.Write("hehe".getBytes());} Catch (FileNotFoundExceptione) {e.Printstacktrace();} Catch (IOExceptione) {e.Printstacktrace();}finally{ Try { if (FileOutputStream!= NULL)FileOutputStream.Close(); } Catch (IOExceptione) {e.Printstacktrace(); }}
Store cache files
If you want to cache some files, you can use Createtempfile () to create the file, you should use Getcachedir () to open the file.
Tip: Under normal circumstances, the above files can not be seen, to see the need to use ADB, Simulator: ADB devices view device
~$ ADB devices
List of devices attached
emulator-5554 Device
Then go to the super user and you can make the appropriate commands to view the
~$ adb-s emulator-5554 Shell
#
Note: The application's internal storage directory is made with the application's package name, and by default, other programs are not able to access the internal
Save the path, unless you display the use of a readable or writable mode.
Resources:
http://developer.android.com/guide/topics/data/data-storage.html
http://developer.android.com/training/basics/data-storage/files.html