Android Data Storage _ internal storage

Source: Internet
Author: User

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

           

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.