Reputation: 19
Is it possible to create a folder on a Unix server with read-write executable permission on that folder using Java code?
I have found Java example code where permissions were given only to files, not to folders.
Upvotes: 1
Views: 117
Reputation: 1592
https://docs.oracle.com/javase/tutorial/essential/io/dirs.html#create
You can create a new directory by using the createDirectory(Path, FileAttribute) method. If you don't specify any FileAttributes, the new directory will have default attributes. For example:
Path dir = ...; Files.createDirectory(path);
The following code snippet creates a new directory on a POSIX file system that has specific permissions:
Set<PosixFilePermission> perms =
PosixFilePermissions.fromString("rwxr-x---");
FileAttribute<Set<PosixFilePermission>> attr =
PosixFilePermissions.asFileAttribute(perms); Files.createDirectory(file, attr);
To create a directory several levels deep when one or more of the parent directories might not yet exist, you can use the convenience method, createDirectories(Path, FileAttribute). As with the createDirectory(Path, FileAttribute) method, you can specify an optional set of initial file attributes. The following code snippet uses default attributes:
Upvotes: 2