Android Learning Note ($): File storage

Source: Internet
Author: User

Java provides us with a complete set of IO streaming systems that allow us to access and store files that are still available on Android. On the Java IO Stream system, there is no more introduction.

The Android system supports not only standard Java IO classes and imitations, but also functions that simplify the process of reading and writing streaming files, mainly to introduce the following two functions:

Openfileoutput ()

Openfileinput ()

(1) Public FileOutputStream openfileoutput (String name, int mode) Create and write files

Open the output stream for the name file under the application's Data folder. Write to the file using Java.

If the specified file exists, open the file directly to prepare the data for writing.

If the specified file does not exist, create a private file that can only be accessed by itself. The file is saved by default in the/data/data/<package name>/file directory of the Android system.

The mode parameter specifies the pattern of the open file, as the following values are optional:

Mode_private : Private mode, files can only be accessed by the program that created the file.

Mode_append : Append mode, if the file already exists, add new data at the end of the file.

Mode_world_readable : Global read mode, which allows any program to read private files.

Mode_world_writeable : Global write mode, allows any program to write private files.

Write () : writes data into the file.

Flush () : Writes the data in the buffer into the file.

Close () : FileOutputStream off .

(2)publicfileinputstream openfileinput (String name) reads the file

Open the input stream for the name file under the application's Data folder. The file is read in the same way that Java reads the file.

(3) In addition to this main two elements, the context also provides the following methods for us to access the file:

Getdir (String name, int mode): Gets or creates a subdirectory corresponding to name.

File Getfilesdir (): Gets the absolute path to the Data folder.

String[] FileList (): Returns all files under the Data folder.

DeleteFile (String name): Deletes the specified file.

(4) Read the files on the SD card

In order to read the files on the SD card, we need to add read/write SD card permissions in androidmanifest.xml :

<!-- permissions to create and delete files in the SD card--

<user-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

<!-- permission to write data to the SD card--

<user-permission android:name= "Android.permission.WRITE_EXTERANL_STORAGE"/>

The steps to read the files on the SD card are as follows:

1. Call environment 's getexternalstoragestate () method to determine if the phone has an SD card, Whether the application has permission to read and write to the SD card, such as environment.getexternalstoragestate (environment.media_mounted) .

2. Call environment 's getexternalstoragedirectory () method to get the SD Card's directory.

3. Use fileinputstream,fileoutputstream,filereader to obtain FileWriter Read and write files on SD card.

Android Learning Note ($): File storage

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.