gpt4 book ai didi

java - 设置文件的执行权限

转载 作者:太空宇宙 更新时间:2023-11-04 05:43:54 24 4
gpt4 key购买 nike

我需要使用 java 1.4 在 Linux 脚本文件上设置执行权限。我更喜欢使用java native 库。如果 native 库无法实现,那么什么是轻量级库可以做到这一点?

最佳答案

JAVA5 及之前版本有使用 exec 提取的解决方法 from here :

public static void runCmd (String[] cmd) {

try {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader r = new BufferedReader(
new InputStreamReader (
p.getInputStream()
)
);
} catch(Exception e) {
}
}

使用

runCmd(new String[] {
"/bin/chmod",
"755",
"/path/to/your/script"
});

By terminal in Linux

最高JAVA6
对于权限使用 File::setExecutable(boolean [, boolean])

File file = new File("/your/path/to/file/file_script.sh");
file.setExecutable(true);

检查这个MKYONG permissions tutorial

关于java - 设置文件的执行权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32452835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com