bmargulies bmargulies - 1 year ago 87
Java Question

How to map File.setReadable to NIO Path operations

What is the 'new' way to accomplish

File.setReadable
independent of platform? Is it PosixFilePermissions or something else?

SMA SMA
Answer Source

You should use setPosixFilePermissions method of Files class like:

Set<PosixFilePermission> permissions = new HashSet<PosixFilePermission>();
permissions.add(PosixFilePermission.OWNER_READ);
permissions.add(PosixFilePermission.OWNER_WRITE);
permissions.add(PosixFilePermission.OWNER_EXECUTE);
permissions.add(PosixFilePermission.GROUP_READ);
permissions.add(PosixFilePermission.GROUP_WRITE);
permissions.add(PosixFilePermission.GROUP_EXECUTE);
permissions.add(PosixFilePermission.OTHERS_READ);
permissions.add(PosixFilePermission.OTHERS_WRITE);
permissions.add(PosixFilePermission.OTHERS_EXECUTE);
Files.setPosixFilePermissions(Paths.get("file.ext"), permissions);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download