The File for Android data storage

Source: Internet
Author: User

Advantages: 1. Suitable for game storage, can store large data;
2. Not only can be stored in the system, but also can be stored in the SD card!

@ Save as: Stream data stream mode
* @ Note 1: By default, files created using the Openfileoutput method can only be used by the app that they call.
* Other apps can't read this file, if need to share data in different applications;
*
* @ NOTE 2: Because the Android OS internal Flash memory is limited, so it is suitable to save less data, of course, we also have a solution to the method,
* Just keep the data in the SD open, so you can!
*
* @ Reminder 1 The specified file does not exist when calling FileOutputStream, and Android will automatically create it.
* Also, by default, the contents of the original file will be overwritten if you want to write the new
* After attaching to the original file content, you can specify its MODE as Context.mode_append.
* Openfileinput ("Save.data") throws an exception when the specified file does not exist, so the code part needs to catch the exception.
*
* @ Reminder 2 Here I will tell you two ways, one is the original ecological file stream to write/read into,
* Another is to use Data flow wrapper file stream to write/read in fact, the data stream to wrap the operation;
* Reason: More write/read operations are supported after packaging, such as: file stream write not supported
* WRITEUTF (String str); But the data is packaged and then supported.
* Finally, all flows need to be closed after the operation is completed.
*
* @ operation mode: context.mode_private: New content overwrites original content
* Context.mode_append: After new content is appended to the original content
* Context.mode_world_readable: Allow other applications to read
* Context.mode_world_writeable: Allows other applications to write, overwriting the original data.

--------------the simple way to read in Android using file-----------------//You can also use file File = new files (path);//The path defaults to the/data/data/package/files, and the master package name can be viewed androidmanifest.xml the packages, or through Context.getfilesdir (). GetAbsolutePath () 
FileInputStream fis = this.openfileinput ("Save.data");Bytearrayoutputstream ByteArray =NewBytearrayoutputstream ();byte[] buffer =New byte[1024];intLen = 0; while(len = fis.read (buffer)) > 0) {bytearray.write (buffer,0, Len);} String Temp=bytearray.tostring (); --------------the way in which the data stream is wrapped in Android-----------------FileInputStream fis= This. Openfileinput ("Save.himi");D atainputstream dis=NewDataInputStream (FIS); String Temp=Dis.readutf (); //write the grass SD card is needed to determine whether there isenvironment.getexternalstoragestate ()--------------the simple way to write using file in Android--------------FileOutputStream Fos=NewFileOutputStream (f); Fos.write (Str.getbytes ()); --------------the way data wrappers in Android later write--------------FileOutputStream Fos= This. Openfileoutput ("Save.himi", mode_private); //Note: If this is a system path, Android will create one by default when the file is not there! But when we put the SD card, we should create the directory path and file! //if (environment.getexternalstoragestate () = null) {//This method is used to test whether the terminal has sdcard!//log.v ("Himi", "SD card");//file Path = new file ("/sdcard/package name");//Create a directory//file F = new file ("/sdcard/package name/save.himi");//Create a file//if (!path.exists ()) {//directory does not exist return false//path.mkdirs ();//Create a directory// }//if (!f.exists ()) {//file does not exist return false//f.createnewfile ();//Create a file// }//fos = new FileOutputStream (f);//storing data in an SD card// } DataOutputStream dos =Newdataoutputstream (FOS);d Os.writeutf (str.tostring ()); //API specifies that the encoding of the UTF-8 format must be written when writing a string//String content = encodingutils.getstring (buffer, "Utf-8″"); This also can be the character array transcoding system!

Here why we use data flow to packaging, in fact, not only to obtain more operation, the most important is convenient and fast, you like to use file to read into the time, obviously complicated some do not say, it also disposable all the data are taken out, not easy to the data processing!
Note To add the SD card read and Write permissions in Androidmanifest.xml <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/ >


The File for Android data 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.