代码
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;
public class Main {
public static void main(String[] args) throws IOException {
File file = new File("/home");
for (File listFile : file.listFiles()) {
Path path = Paths.get(listFile.getAbsolutePath());
Set<PosixFilePermission> set = Files.getPosixFilePermissions(path);
int group = 0, own = 0, other = 0;
for (PosixFilePermission permission : set) {
switch (permission) {
case GROUP_READ:
group += 4;
break;
case GROUP_WRITE:
group += 2;
break;
case GROUP_EXECUTE:
group += 1;
break;
case OWNER_READ:
own += 4;
break;
case OWNER_WRITE:
own += 2;
break;
case OWNER_EXECUTE:
own += 1;
break;
case OTHERS_READ:
other += 4;
break;
case OTHERS_WRITE:
other += 2;
break;
case OTHERS_EXECUTE:
other += 1;
break;
}
}
int code = own * 100 + group * 10 + other;
System.out.println(code);
}
}
结果
可以用stat -c %a
来验证结果