In Linux, each file has a permission attribute. In Android, how does one use Java to change the permission of a file?
There are two ways to change the File Permission In Android
1. Use the openfileoutput method:
[Java]View
Plaincopy
- Fileoutputstream Fos;
- Fos = openfileoutput ("FILENAME", mode_world_readable );
Fileoutputstream Android. content. contextwrapper. openfileoutput (string name,
Int mode) throwsfilenotfoundexception
Open a private file associated with this context's application package for writing. Creates the file if it doesn't already exist.
The available mode parameters are as follows:
/**
* File Creation Mode: the default mode, where the created file can only
* Be accessed by the calling application (or all applications sharing
* Same user ID ).
* @ See # mode_world_readable
* @ See # mode_world_writeable
*/
Public static final int mode_private = 0x0000;
/**
* File Creation Mode: Allow all other applications to have read access
* To the created file.
* @ See # mode_private
* @ See # mode_world_writeable
*/
Public static final int mode_world_readable = 0x0001;
/**
* File Creation Mode: Allow all other applications to have write access
* To the created file.
* @ See # mode_private
* @ See # mode_world_readable
*/
Public static final int mode_world_writeable = 0x0002;
/**
* File Creation Mode: for use with {@ link # openfileoutput}, if the file
* Already exists then write data to the end of the existing file
* Instead of erasing it.
* @ See # openfileoutput
*/
Public static final int mode_append = 0x8000;
In fact, this method eventually calls the system's chmod to implement the function of changing file permissions.
However, this method has limitations. The files created by this method can only be located in the private directory of the program, that is,/data/APP-package/files/
2. Use runtime.getruntime(cmd.exe C ()
[C-sharp]View
Plaincopy
- Runtime.getruntime(cmd.exe C ("chmod 644" + filename );
This method calls the system command chmod to change the File Permission. To determine the return value of the command, it is best to write it:
[Java]View
Plaincopy
- PROCESS p = runtime.getruntime(cmd.exe C ("chmod 644" + filename );
- Int status = P. waitfor ();
- If (status = 0 ){
- // Chmod succeed
- } Else {
- // Chmod failed
- }