File Storage Technology in Android

Source: Internet
Author: User

File Storage Technology in Android

File storage is a function that every system should have. Android is developed in Java, so its file storage function is basically the same as J2SE. Why Is Nas introduced? If we store large files and images only on mobile phones, we generally use an external storage device for storage. Now let's talk about using programming to store data in the Android system.

File storage location

Files are stored in/data/by default/ <包> /Files /***. * ** (for the APK we developed), of course, we can also store it on the memory card (this time needs to be done in androidManifest. xml to set the file operation permissions ).







OpenFileOutput ()Write

This method is provided in the Activity component for writing file data. Let's take a look at the following code and explain it again:

Public void store (){
Try {
// Get the output stream of the file
FileOutputStream out = this. openFileOutput ("test.txt", Context. MODE_WORLD_READABLE );
// Write the value of the textboox1 control on the interface to the output file stream.
Out. write (textboox1.getText (). toString (). getBytes ());
// Close the file stream. The data is saved successfully.
Out. close ();


} Catch (FileNotFoundException e ){


Return;


}


Catch (IOException e ){

Return;


}

}

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

Context. MODE_PRIVATE = 0: the default operation mode, indicating that the file is private data and can only be accessed by the application itself. In this mode, the written content will overwrite the content of the original file, if you want to append the newly written content to the original file. You can use Context. MODE_APPEND;
Context. MODE_APPEND = 32768: the mode checks whether the file exists and appends the content to the file. Otherwise, a new file is created;
Context. MODE_WORLD_READABLE = 1: indicates that the current file can be read by other applications;
Context. MODE_WORLD_WRITEABLE = 2: indicates that the current file can be written by other applications;

To have the read and write permissions, you must specify Context. MODE_WORLD_READABLE + Context. MODE_WORLD_WRITEABLE.

OpenFileInput () read

The Activity component provides the read method. The default file is in/data/ <包> /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;
// Read the data and put it in the byte array
While (length = in. read (buffer ))! =-1 ){
// Write the 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 data is stored on an external device, you need to set the operation permissions. The following operations are commonly used in the actual project development process.

// 1. Get the status of the SDCard. If the mobile phone has an SDCard and can be read and written, the status returned by the method is Environment. MEDIA_MOUNTED.
If (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED )){
// 2. Get the SDCard directory
File sdCardDir = Environment. getExternalStorageDirectory ();
// 3. Create an external device to obtain the file
File saveFile = new File (sdCardDir, cmdtest.txt ");
// The file is stored in/sdcard/test.txt.
// It is equivalent to File saveFile = new File ("/sdcard/a.txt ");

// 4. file output stream
FileOutputStream outStream = new FileOutputStream (saveFile );

// 5. Write Data
OutStream. write ("test". getBytes ());

// 6. close the file output stream
OutStream. close ();
}

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.