Android data storage _ internal storage

Source: Internet
Author: User

Download source code (download-free credits): Download

You can directly store data in internal storage. By default, files are private to internal storage and cannot be
When you uninstall an application, the files are removed.
You can create and write data in two ways:

  • Using related methods in java,
  • Use the related methods in android. content,
    • Call openFileOutput () and return the FileOutputStream object.
    • Call the write () method of the FileOutputStream object
    • Close stream

      Reading files is also basically the same method.
      Tips for reading files: If you want to save a static file in your application during compilation, save the file
      Res/raw/directory. You can use openRawResource () to open a file and return an InputStream.
      Object, and then you can read and write data.

      Code:
      Method 1:

      FileOutputStream writeStream = null; FileOutputStream fileOutputStream = null; switch (view. getId () {case R. id. button1: // other files created by other programs cannot access File file1 = new File (getFilesDir (), FILE1); try {// write data writeStream = new FileOutputStream (file1); writeStream. write ("haha ". getBytes ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {try {if (wr IteStream! = Null) writeStream. close ();} catch (IOException e) {e. printStackTrace ();}}

      Method 2:

      // Android is used. content, try {/* is set to MODE_PRIVATE and cannot be accessed by other programs. This is the default constructor and can be set to other methods for read/write. */FileOutputStream = openFileOutput (FILE2, Context. MODE_PRIVATE); // write data fileOutputStream. write ("hehe ". getBytes ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {try {if (fileOutputStream! = Null) fileOutputStream. close ();} catch (IOException e) {e. printStackTrace ();}}

      Store cached 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: Normally, the above files cannot be viewed. To view them, you must use adb. simulator: adb devices to view the device.

      ~ $ Adb devices
      List of devices attached
      Emulator-5554 device

      Then, go to the Super User and run the corresponding command to view the information.

      ~ $ Adb-s emulator-5554 shell
      #

      Note: the internal storage directory of the application is determined by the package name of the application. By default, other programs cannot access the internal storage directory.
      Storage path, unless you use readable or writable mode.

      References:

      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.