In depth understanding of file operation mode developed by Android

  • 2020-05-07 20:22:37
  • OfStack

1. Basic concept
 
//  Context object  
private Context context; 
public FileService(Context context) 
{ 
super(); 
this.context = context; 
} 
//  Save file method  
public void save(String filename, String fileContent) throws Exception 
{ 
FileOutputStream fos = context.openFileOutput(filename, context.MODE_PRIVATE); 
fos.write(fileContent.getBytes("UTF-8")); 
fos.close(); 
} 

private mode
Can only be accessed by the current application that created the file
If the file does not exist will create a file; If the created file already exists, the original file is overwritten
Context.MODE_PRIVATE = 0;
append mode
(1) private
If the file does not exist will create a file; Appends to the end of the file if it exists
Context.MODE_APPEND = 32768;
readable mode
The file created can be read by other applications
Context.MODE_WORLD_READABLE=1;
writable mode
Allows other applications to write to it.
Context.MODE_WORLD_WRITEABLE=2
2. Use in combination
 
FileOutputStream outStream = this.openFileOutput("xy.txt",Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE); 

Allows other applications to read and write and override by default
 
FileOutputStream outStream = this.openFileOutput("xy.txt",Context.MODE_APPEND+Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE); 

Append mode, but allow other applications to read and write

Related articles: