File storage technology in Android

Source: Internet
Author: User

File storage is a feature that every system should have, and the Android system is developed in the Java language, so its file storage functions are basically the same as J2SE. Why is file storage introduced? Because large files, pictures, etc. if we only store in the mobile phone is very limited, is generally used with an external storage device for storage. Well, today we're going to talk about storing it programmatically in an Android system.

location of the file store

The generic file is stored by default in the/data/data/< package >/files/***.*** (for the apk we developed), and of course we can also store it on the memory card (this time requires permission to set the file in Androidmanifest.xml).

<!--Create and delete files in SDcard permissions--
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

<!--write data to sdcard permissions--
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>


openfileoutput () Write

This method is provided in the activity component for writing file data, so let's look at the following code and then explain:

 public Void Store () {
        try {
           //Get output stream of file
            FileOutputStream out=this.openfileoutput (" Test.txt ", context.mode_world_readable);
           //writes the value of the Textboox1 control on the interface to the output file stream
            OU T.write (Textboox1.gettext (). toString (). GetBytes ());
           //close file stream, data saved successfully
    Out.close ();


       } catch (FileNotFoundException e) {


            return;


       


        catch (IOException e) {

  &NB Sp         return;


       

 } 

From the code we can see that openfileoutput () has two parameters, the first is the file name, and the second is the operation permission of the file. It has the following operations permissions:

Context.mode_private=0: As the default mode of operation, which means that the file is private data and can only be accessed by the app itself, in which the content of the write overwrites the contents of the original file, if you want to append the newly written content to the original file. can use Context.mode_append;
Context.mode_append = 32768: The mode checks whether the file exists, appends content to the file, or creates a new file;
Context. mode_world_readable = 1: Indicates that the current file can be read by another application;
Context. Mode_world_writeable= 2: Indicates that the current file can be written by another application;

If you want to have read and write permissions, you need to have the Context. mode_world_readable+Context. Mode_world_writeable said.

openfileinput () Read

The activity component provides a read method, the default file under the/data/data/< package >/files/, the instance code is as follows:

public void onload ()


{


try {
File input stream
FileInputStream in=this.openfileinput ("test.txt");


Bytearrayoutputstream stream=new Bytearrayoutputstream ();


Byte[] Buffer=new byte[1024];


int length=-1;
Reads the data and places it in a byte array
while ((Length=in.read (buffer))!=-1) {
Writes a byte file stream
Stream.Write (buffer,0,length);


}

Stream.Close ();
In.close ();
Textboot1.settext (Stream.tostring ());
} catch (FileNotFoundException e) {
E.printstacktrace ();

}
catch (IOException e) {


return;


}
}

sdcard Data Storage

As mentioned earlier, when storing data in an external device, you need to set its operation permissions. The following methods of operation are commonly used in the actual project development process.

1. Used to get the status of SDcard, if the phone is equipped with sdcard, and can read and write, then the state of the method returned is equal to environment.media_mounted
if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {
2. Get the SDcard directory
File Sdcarddir = Environment.getexternalstoragedirectory ();
3. Create a Get file on an external device
File SaveFile = new file (Sdcarddir, "test.txt");
File storage location in/sdcard/test.txt
Equivalent to file SaveFile = new file ("/sdcard/a.txt");

4. File output stream
FileOutputStream OutStream = new FileOutputStream (saveFile);

5. Writing data
Outstream.write ("Test". GetBytes ());

6. Close the file output stream
Outstream.close ();
}

File storage technology in Android

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.