Java Creating files and directories with a certain owner (user/group)
Solution 1
You can achieve JDK 7 (Java NIO)
Use setOwner() method....
public static Path setOwner(Path path,
UserPrincipal owner)
throws IOException
Usage Example: Suppose we want to make "joe" the owner of a file:
Path path = ...
UserPrincipalLookupService lookupService =
provider(path).getUserPrincipalLookupService();
UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
Files.setOwner(path, joe);
Get more information about same from below url
http://docs.oracle.com/javase/tutorial/essential/io/file.html
Example : Set Owner
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.UserPrincipal;
import java.nio.file.attribute.UserPrincipalLookupService;
public class Test {
public static void main(String[] args) throws Exception {
Path path = Paths.get("C:/home/docs/users.txt");
FileOwnerAttributeView view = Files.getFileAttributeView(path,
FileOwnerAttributeView.class);
UserPrincipalLookupService lookupService = FileSystems.getDefault()
.getUserPrincipalLookupService();
UserPrincipal userPrincipal = lookupService.lookupPrincipalByName("mary");
Files.setOwner(path, userPrincipal);
System.out.println("Owner: " + view.getOwner().getName());
}
}
Example : GetOwner
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.UserPrincipal;
public class Test {
public static void main(String[] args) throws Exception {
Path path = Paths.get("C:/home/docs/users.txt");
FileOwnerAttributeView view = Files.getFileAttributeView(path,
FileOwnerAttributeView.class);
UserPrincipal userPrincipal = view.getOwner();
System.out.println(userPrincipal.getName());
}
}
Solution 2
You can achieve this in JDK 7 using new IO API (Java NIO)
There are getOwner()/setOwner() methods available to manage ownership of files, and to manage groups you can use PosixFileAttributeView.setGroup()
The following code snippet shows how to set the file owner by using the setOwner method:
Path file = ...;
UserPrincipal owner = file.GetFileSystem().getUserPrincipalLookupService()
.lookupPrincipalByName("sally");
Files.setOwner(file, owner);
There is no special-purpose method in the Files class for setting a group owner. However, a safe way to do so directly is through the POSIX file attribute view, as follows:
Path file = ...;
GroupPrincipal group =
file.getFileSystem().getUserPrincipalLookupService()
.lookupPrincipalByGroupName("green");
Files.getFileAttributeView(file, PosixFileAttributeView.class)
.setGroup(group);
To Kra
I am passionate programmer/developer : currently working as JavaEE backend developer. Before I worked on Eclipse RCP/SWT applications. I love OSGi & RCP development I do also dig into Android Java Development My specialty: Regular Expression (love them so much).
Updated on June 14, 2022Comments
-
To Kra almost 2 years
Is possible in Java to manage creating files / directories (if program runs as ROOT) with different user/group ?