Java使用特定所有者(用户/组)创建文件和目录 [英] Java Creating files and directories with a certain owner (user/group)

查看:215
本文介绍了Java使用特定所有者(用户/组)创建文件和目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中可以用不同的用户/组来管理创建文件/目录(如果程序运行为ROOT)吗? 解决方案

您可以实现JDK 7(Java NIO)

使用setOwner()方法....

 public static Path setOwner(Path path,
UserPrincipal owner)
throws IOException

用法示例:假设我们想让joe成为文件的所有者:

  Path path = ... 
UserPrincipalLookupService lookupService =
provider(path).getUserPrincipalLookupService();
UserPrincipal joe = lookupService.lookupPrincipalByName(joe);
Files.setOwner(path,joe);

从以下URL获取更多相关信息

http://docs.oracle.com/javase/tutorial/essential /o/file.html



示例:设置所有者

  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;
$ b $ public class Test {

public static void main(String [] args)throws Exception {
Path path = Paths.get(C:/ home /文档/ 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());




$ b

示例: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;
$ b $ public class Test {
public static void main(String [] args)throws Exception {
Path path = Paths.get(C:/ home / docs / users。文本);
FileOwnerAttributeView view = Files.getFileAttributeView(path,
FileOwnerAttributeView.class);
UserPrincipal userPrincipal = view.getOwner();
System.out.println(userPrincipal.getName());
}
}


Is possible in Java to manage creating files / directories (if program runs as ROOT) with different user/group ?

解决方案

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());
  }
}

这篇关于Java使用特定所有者(用户/组)创建文件和目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆