1 打开sudo 终端
runtime.exec("echo 你的root密码 | sudo -S 你的命令");
2 创建不同操作系统目录
File fp = new File(lastsFolderPath);
// 创建目录
if (!fp.exists()) {
fp.mkdirs();// 目录不存在的情况下,创建目录。
if (-1 != System.getProperties().getProperty("os.name")
.toLowerCase().indexOf("windows")) {
// 1 windows OS:通过io File类对文件路径赋予读写权限
if (!fp.canRead()) {
fp.setReadable(true);
}
if (!fp.canWrite()) {
fp.setWritable(true);
}
if (!fp.canExecute()) {
fp.setExecutable(true);
}
} else {
// 1 2 其它操作系统
// :通过Runtime.getRuntime().exec()执行command对文件路径赋予读写权限
String filepath = lastsFolderPath;
String command = "chmod -R 755 " + filepath;
@SuppressWarnings("unused")
Runtime runtime = Runtime.getRuntime();
try {
@SuppressWarnings("unused")
Process exec = Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
}
}
}