Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中创建文件时如何设置文件所有者/组_Java_Unix_Posix - Fatal编程技术网

在Java中创建文件时如何设置文件所有者/组

在Java中创建文件时如何设置文件所有者/组,java,unix,posix,Java,Unix,Posix,我想设置从Java创建的文件的(unix)所有者和组。我想要一些像: Path文件=。。。; 设置perms=PosixFilePermissions.fromString(“rwxr-x--”); FileAttribute attr=PosixFilePermissions.asFileAttribute(perms); Files.createFile(file,attr); --这是一个如何设置权限的示例,但我找不到如何对所有者/组执行相同的操作 请注意,我不想在创建文件后更改所有者(

我想设置从Java创建的文件的(unix)所有者和组。我想要一些像:

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或组