How to set rw- r— r— permissions programmatically?

纵饮孤独 提交于 2019-11-30 20:27:16
Process process = null;
DataOutputStream dataOutputStream = null;

try {
    process = Runtime.getRuntime().exec("su");
    dataOutputStream = new DataOutputStream(process.getOutputStream());
    dataOutputStream.writeBytes("chmod 644 FilePath\n");
    dataOutputStream.writeBytes("exit\n");
    dataOutputStream.flush();
    process.waitFor();
} catch (Exception e) {
    return false;
} finally {
    try {
        if (dataOutputStream != null) {
            dataOutputStream.close();
        }
        process.destroy();
    } catch (Exception e) {
    }
}

The value is wrong, the correct one is 420 (420 decimal is 644 octal). Alternatively you can add a leading 0 to make it a java octal literal. i.e.

chmod(destinationFile, 0644)

You should be able to set these permissions (rw- r-- r--) on the file using:

path.setReadOnly(true); //Sets all permissions for every owner back to read-only
path.setWritable(true); //Sets the owner's permissions to writeable

where path is your File object.

You shouldn't need to use FileUtils with reflection to set permissions on a file. You can just use the helper methods on the File class. With File, you can also call: setReadable(), setWritable(), and setExecutable()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!