我正在尝试更改文件夹和文件夹中所有文件的文件权限。以下代码适用于该文件夹。我可以设置任何参数来更改文件夹中文件的权限。
private void setPOSIXFilePermissions(String filePath) {
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
perms.add(PosixFilePermission.OWNER_EXECUTE);
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.OTHERS_READ);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
try {
java.nio.file.Files.setPosixFilePermissions(Paths.get(filePath), perms);
} catch (IOException e) {
LOGGER.error("Failed to set file permission.", e);
}
}
答案 0 :(得分:-1)
试试这个:
// Use: - recursive = true to change permissions for all sub directories and sub files
// - recursive = false for only one folder and its sub files.
public static boolean givePerms(File f, Set<PosixFilePermission> perms, boolean recursive)
{
boolean success = givePerm(f, perms);
if (f.isDirectory() && recursive)
{
File[] files = f.listFiles();
for (File file : files)
{
if (!givePerms(file, perms, recursive)) success = false;
}
}
return success;
}