Android data storage-file storage

Source: Internet
Author: User

The method for reading/writing files in Android is the same as that in Java I/O. openfileinput () and openfileoutput () methods are provided to read files on the device. However, by default, files cannot be different.ProgramShared between the two methods, the above two methods only support reading files under the application directory, reading files not under its own directory will throw a filenotfoundexception exception. The created file is stored in the/data/<package name>/Files directory.

Return Value

Function

Remarks

Fileinputstream

Openfileinput (string name)

Open the output stream of the specified private file.

Name: The name of the file to be opened. It cannot contain path separators.

Fileoutputstream

Openfileoutput (string name, int Mode)

Open the output stream of the specified private file.

Name: The name of the file to be opened. It cannot contain path separators.

Mode: Operation mode. The default value is 0 or mode_private. Other modes include:

L mode_append: append

L mode_world_readable

L mode_world_writeable

If the specified file does not exist when you call this function, a blank file is automatically created.

By default, the content of the original file will be overwritten during writing. To append the newly written content to the end of the original file, you can specify the mode as context. mode_append.

Boolean

Deletefile (string name)

Deletes a specified object.

Name: The name of the file to be deleted. It cannot contain a path.

String []

Filelist ()

Returns the file names of all private files related to the application package that calls the context related to this function.

ExampleCode:

// Read the file

Fileinputstream FCM = openfileinput(“text1.txt ");

... ...

FCM. Close (); // close the input stream

// Write a file

Fileoutputstream Fos = openfileoutput(export text1.txt ", mode_append );

... ...

FOS. Close (); // close the output stream

If you need to load the content of a file to initialize the program during application development, you can set the file to static before compiling the program) put the file in the Res/raw/mydatafile directory, and then use resources. openrawresource (R. raw. file Name) This method returns an inputstream object. The sample code is as follows:

Resources myresources = getresources ();

Inputstream myfile = myresources. openrawresource (R. Raw. myfilename );

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.