在Java中创建文件时如何设置文件所有者/组
我想设置从Java创建的文件的(unix)所有者和组。我想要一些像:在Java中创建文件时如何设置文件所有者/组,java,unix,posix,Java,Unix,Posix,我想设置从Java创建的文件的(unix)所有者和组。我想要一些像: Path文件=。。。; 设置perms=PosixFilePermissions.fromString(“rwxr-x--”); FileAttribute attr=PosixFilePermissions.asFileAttribute(perms); Files.createFile(file,attr); --这是一个如何设置权限的示例,但我找不到如何对所有者/组执行相同的操作 请注意,我不想在创建文件后更改所有者(
Path文件=。。。;
设置perms=PosixFilePermissions.fromString(“rwxr-x--”);
FileAttribute attr=PosixFilePermissions.asFileAttribute(perms);
Files.createFile(file,attr);
--这是一个如何设置权限的示例,但我找不到如何对所有者/组执行相同的操作
请注意,我不想在创建文件后更改所有者(这已经得到了回答),而是在创建文件时更改所有者
这个问题的动机是,在我设置正确的所有者和权限时,我需要确保我正在创建的文件没有被其他用户修改。描述了如何设置和获取符合posix标准的所有者
Path path = ...
UserPrincipalLookupService lookupService =
provider(path).getUserPrincipalLookupService();
UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
Files.setOwner(path, joe);
函数原型如下所示:
public static Path setOwner(Path path,
UserPrincipal owner)
throws IOException
参数:
-定位文件的文件引用路径
-新文件所有者所有者
File originalFile = new File("original.jpg"); // just as an example
GroupPrincipal group = Files.readAttributes(originalFile.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS).group();
File targetFile = new File("target.jpg");
Files.getFileAttributeView(targetFile.toPath(), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(group);
设置文件的组所有者
File originalFile = new File("original.jpg"); // just as an example
GroupPrincipal group = Files.readAttributes(originalFile.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS).group();
File targetFile = new File("target.jpg");
Files.getFileAttributeView(targetFile.toPath(), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(group);
描述了如何设置和获取posix conform所有者
Path path = ...
UserPrincipalLookupService lookupService =
provider(path).getUserPrincipalLookupService();
UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
Files.setOwner(path, joe);
函数原型如下所示:
public static Path setOwner(Path path,
UserPrincipal owner)
throws IOException
参数:
-定位文件的文件引用路径
-新文件所有者所有者
File originalFile = new File("original.jpg"); // just as an example
GroupPrincipal group = Files.readAttributes(originalFile.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS).group();
File targetFile = new File("target.jpg");
Files.getFileAttributeView(targetFile.toPath(), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(group);
设置文件的组所有者
File originalFile = new File("original.jpg"); // just as an example
GroupPrincipal group = Files.readAttributes(originalFile.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS).group();
File targetFile = new File("target.jpg");
Files.getFileAttributeView(targetFile.toPath(), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(group);
描述了如何设置和获取posix conform所有者
Path path = ...
UserPrincipalLookupService lookupService =
provider(path).getUserPrincipalLookupService();
UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
Files.setOwner(path, joe);
函数原型如下所示:
public static Path setOwner(Path path,
UserPrincipal owner)
throws IOException
参数:
-定位文件的文件引用路径
-新文件所有者所有者
File originalFile = new File("original.jpg"); // just as an example
GroupPrincipal group = Files.readAttributes(originalFile.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS).group();
File targetFile = new File("target.jpg");
Files.getFileAttributeView(targetFile.toPath(), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(group);
设置文件的组所有者
File originalFile = new File("original.jpg"); // just as an example
GroupPrincipal group = Files.readAttributes(originalFile.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS).group();
File targetFile = new File("target.jpg");
Files.getFileAttributeView(targetFile.toPath(), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(group);
描述了如何设置和获取posix conform所有者
Path path = ...
UserPrincipalLookupService lookupService =
provider(path).getUserPrincipalLookupService();
UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
Files.setOwner(path, joe);
函数原型如下所示:
public static Path setOwner(Path path,
UserPrincipal owner)
throws IOException
参数:
-定位文件的文件引用路径
-新文件所有者所有者
File originalFile = new File("original.jpg"); // just as an example
GroupPrincipal group = Files.readAttributes(originalFile.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS).group();
File targetFile = new File("target.jpg");
Files.getFileAttributeView(targetFile.toPath(), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(group);
设置文件的组所有者
File originalFile = new File("original.jpg"); // just as an example
GroupPrincipal group = Files.readAttributes(originalFile.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS).group();
File targetFile = new File("target.jpg");
Files.getFileAttributeView(targetFile.toPath(), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(group);
似乎无法在文件创建时设置所有权。当您查看的文档时,它描述了如何设置文件权限,但唯一提到的所有者是: 如果文件不存在,将创建该文件。文件的所有者(用户ID)设置为进程的有效用户ID。组所有权(组ID)设置为进程的有效组ID或父目录的组ID 另见 我最终的解决方案是:
000
:
Path file = ...;
Set<PosixFilePermission> perms = Collections.<PosixFilePermissions>emptySet();
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
Files.createFile(file, attr);
Path文件=。。。;
Set perms=Collections.emptySet();
FileAttribute attr=PosixFilePermissions.asFileAttribute(perms);
Files.createFile(file,attr);
这将确保其他用户无法在任何时间点修改文件。在文件创建时设置所有权似乎是不可能的。当您查看的文档时,它描述了如何设置文件权限,但唯一提到的所有者是: 如果文件不存在,将创建该文件。文件的所有者(用户ID)设置为进程的有效用户ID。组所有权(组ID)设置为进程的有效组ID或父目录的组ID 另见 我最终的解决方案是:
000
:
Path file = ...;
Set<PosixFilePermission> perms = Collections.<PosixFilePermissions>emptySet();
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
Files.createFile(file, attr);
Path文件=。。。;
Set perms=Collections.emptySet();
FileAttribute attr=PosixFilePermissions.asFileAttribute(perms);
Files.createFile(file,attr);
这将确保其他用户无法在任何时间点修改文件。在文件创建时设置所有权似乎是不可能的。当您查看的文档时,它描述了如何设置文件权限,但唯一提到的所有者是: 如果文件不存在,将创建该文件。文件的所有者(用户ID)设置为进程的有效用户ID。组所有权(组ID)设置为进程的有效组ID或父目录的组ID 另见 我最终的解决方案是:
000
:
Path file = ...;
Set<PosixFilePermission> perms = Collections.<PosixFilePermissions>emptySet();
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
Files.createFile(file, attr);
Path文件=。。。;
Set perms=Collections.emptySet();
FileAttribute attr=PosixFilePermissions.asFileAttribute(perms);
Files.createFile(file,attr);
这将确保其他用户无法在任何时间点修改文件。在文件创建时设置所有权似乎是不可能的。当您查看的文档时,它描述了如何设置文件权限,但唯一提到的所有者是: 如果文件不存在,将创建该文件。文件的所有者(用户ID)设置为进程的有效用户ID。组所有权(组ID)设置为流程的有效组ID或组