Android调用Linux命令修改文件权限的两种实现方式

时间:2022-07-31 16:47:11
public class FileMode {
    public static void changeFileModeByCmd(String file) {
        String[] command = {"chmod", "777", file};
        ProcessBuilder builder = new ProcessBuilder(command);
        try {
            builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void changeFileModeBySys(String filename) {
        try {
            Process p = Runtime.getRuntime().exec("chmod 644 " + filename);
            int status = p.waitFor();
            if (status == 0) {
                //chmod succeed
            } else {
                //chmod failed
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}