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

第一种:
public class FileMode {
    public static void changeFileModeByCmd(String file) {
        String[] command = {"chmod", "777", file}; //777代表可读写可执行,666代表可读写不可执行
        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();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_33718648/article/details/79727034
今日推荐