How to change the permissions of a file in Android Java code

Source: Internet
Author: User

Under Linux every file has a permission attribute, then how to use Java to change the permissions of a file in Android?

There are two ways to change the permissions of a file in Android

1. Using the Openfileoutput method:

fileoutputstream Fos;   = Openfileoutput ("filename", mode_world_readable);

FileOutputStream android.content.ContextWrapper. Openfileoutput (String name, int mode) throwsfilenotfoundexception

Open a private file associated with the 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 the
* Same user ID).
* @see #MODE_WORLD_READABLE
* @see #MODE_WORLD_WRITEABLE
*/
public static final int mode_private = 0x0000;
/**
* File creation Mode:allow All and applications to has 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 and applications to has 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, the method finally calls the system's chmod to realize the function of changing the file permission.

However, the method has limitations, and the file he creates can only be located in the program's private directory, which is/data/data/app-package/files/

2. Using Runtime.getruntime (). EXEC ()

Runtime.getruntime (). EXEC ("chmod 644" + filename);

The method calls the system command chmod to change the permissions of the file, in order to be able to determine the return value of the command, it is best to write:

Process p = runtime.getruntime (). EXEC ("chmod 644" + filename);   int status = p.waitfor ();   if (Status = = 0)      {//chmod  succeedelse  {      // chmod failed  }  

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.