A summary of reading and writing methods for Android phone files in memory

Source: Internet
Author: User

a summary of reading and writing methods for Android phone files in memory

This article mainly introduces the Android phone in memory file reading and writing methods, the example summarizes the Android for file read and write operation of the relevant skills, very practical value, the need for Friends can refer to:

How to read and write the file data in the phone's memory?

The context provides a way to open the file I/O stream in the application's Data folder as follows:

?

1 FileInputStream openfileinput (String name)

Open the data flow for the name file under the application's Data folder

?

1 Fileoutputsream openfileoutput (String name, int mode)

Open the output stream for the name file under the Application Data folder, mode to specify the pattern of the file open, which has 4 main types:

①mode_private (this file can only be read and written by the current application)

②mode_append (open with Append, can append content in file)

③mode_world_readable (this file content can be read by other applications)

④mode_world_writeable (The contents of this file can be written by other application degrees)

Read file:

Suppose name is the name of the file you want to open

?

1 2 3 4 5 6 7 8 9 FileInputStream f = openfileinput (name); byte[] buf = new byte[1024]; int hasread = 0; StringBuilder sb = new StringBuilder (""); while ((Hasread = F.read (buf) >0)) {sb.append (new String (buf, 0, Hasread));} f.close ();

Get file Content string:

?

1 Sb.tostring ();

Write file:

Suppose the string to be written is content

?

1 2 3 4 FileOutputStream f = openfileoutput (name,mode_append); PrintStream temp = new PrintStream (f); TEMP.PRINTLN (content); Temp.close ();

Note: The data file for the application is saved by default in the/data/datea /files directory, using the Openfileinput and Openfileoutput methods to open the file input stream, when the output stream is open, the file in the application's Data folder. That is, the files in the phone's memory, not the SD card files.

I hope this article will help you with your Android program.

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.