This article summarizes the methods of reading and writing the files in memory of the Android phone. Share to everyone for your reference. The specific analysis is as follows:
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:
FileInputStream openfileinput (String name)
Open the data flow for the name file under the application's Data folder
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
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:
Write file:
Suppose the string to be written is content
FileOutputStream f = openfileoutput (name,mode_append);
PrintStream temp = new PrintStream (f);
TEMP.PRINTLN (content);
Temp.close ();
Note: The application's data files are saved by default in the/data/datea<package name>/files directory, using the Openfileinput and Openfileoutput methods to open the file input stream, the output stream, The files in the application's Data folder are open, that is, files in the phone's memory, not the files in the SD card.
I hope this article will help you with your Android program.