gpt4 book ai didi

Java IOException : Permission denied on linux while running as owner 错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:26:27 25 4
gpt4 key购买 nike

我需要创建一些目录和文件,它们都应该有权限 0600。
当我从 NetBeans 调试运行时:创建目录后,当我尝试在其中存储一些文件时,我收到带有“权限被拒绝”消息的 IOException,而目录和文件都是由同一用户同时使用同一应用程序创建的,所以我认为 0600(所有者读/写)应该工作。
并且在运行 Jar 文件时,chmod 根本不起作用!我的代码是:

if(!Dest.exists()){
boolean res=dirs.mkdirs();
if(res){
Runtime.getRuntime().exec("chmod -R 600 '"+dirs.getAbsolutePath()+"'");
}
}
File Destination=new File(Dest, source.getName());
documentManager.copyFile(source, Destination);

复制文件是:

public static void copyFile(File sourceFile, File destFile) throws FileNotFoundException,IOException {
if(!destFile.exists()) {
destFile.createNewFile();
}

FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
Runtime.getRuntime().exec("chmod 600 '"+destFile.getAbsolutePath()+"'");
destination.close();
}
}
}

有什么问题?

谢谢

最佳答案

该目录需要设置可执行位,以便您将文件写入其中。在目录上尝试 chmod +x。

mkdir tmp2323
chmod a-x tmp2323
touch tmp2323/test
touch: cannot touch `tmp2323/test': Permission denied

关于Java IOException : Permission denied on linux while running as owner 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11737587/

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