Android read/write file: Java-based file input/output stream

Source: Internet
Author: User

Note: Due to typographical issues, correct the changes now.

1. store files to the local default address

 
1 Public VoidSave (string filename, string content)ThrowsException
2{
3Fileoutputstream outstream = context. openfileoutput (filename, context. mode_private );
4Outstream. Write (content. getbytes ());
5Outstream. Close ();
6}

First, create a file output stream object whose value is equal to the return value of the context call openfileoutput () method. This openfileoutput () method will open the specified private file for write operations for this application, if the current file does not exist, one will be created.

The two parameters of the method specify the file name and read/write mode respectively. The file name cannot contain the path, and the file is located in data/<package name>/files.(This directory can be obtained using the getfilesdir () method. Similarly, the getcachedir () method can obtain the cache directory at the same level as files ).There are four basic read/write modes:

Mode_private or 0Private mode: the default mode. files can only be operated by the current application, but are transparent to other applications. If the file already exists, the new data overwrites the original data.

Mode_append or 32768Append mode: the file can only be operated by the current application, but transparent to other applications. If the file already exists, the new data will be added after the old data.

Mode_world_readable or 1Read mode: allows other applications to read files created in this mode.

Mode_world_writeable or 2Write mode: allows other applications to write the files created in this mode.

If you allow other applications to read and write the file, the parameter can beContext. mode_world_readable + context. mode_world_writeable or 3,That is, the combination of read and write.

Then, call the write () Write method of the output stream object. The parameter is of the byte type. to convert the string type to the byte type, call the getbytes () method.

Close the output stream.

 

 

2. store files on the SD card

1 Public VoidSavetosdcard (string filename, string content)ThrowsException
2{
3File file =NewFile (environment. getexternalstoragedirectory (), filename );
4Fileoutputstream outstream =NewFileoutputstream (File );
5Outstream. Write (content. getbytes ());
6Outstream. Close ();
7}

 

First, configure the permission to operate the SD card in the list file.

1<! -- Create and delete file permissions in sdcard -->
2<Uses-Permission Android: Name = "android. Permission. mount_unmount_filesystems"/>
3<! -- Write data to sdcard -->
4<Uses-Permission Android: Name = "android. Permission. write_external_storage"/>

To store a file on the SD card, you must specify a directory. In this case, we no longer call the openfileoutput () method as before, because the file is stored in the "Memory" of the mobile phone itself, the file constructor can be used to input file addresses. There are three common parameters: Directory and file name, absolute path, directory path and file name.

 
1 PublicFile (File Dir, string name)
2 PublicFile (string path)
3 PublicFile (string dirpath, string name)
 

Passed in the caseEnvironment. getexternalstoragedirectory ()Method to get the root directory of the expansion card in the Android mobile phone, and then pass the file as a parameter to the file output stream constructor, and return an output stream. The subsequent operations are the same as the previous ones.

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.