这是代码:
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.nio.file.attribute.UserPrincipal;
import java.nio.file.FileSystems;
import java.lang.UnsupportedOperationException;
import java.io.IOException;
public class SetOwnerOfFile{
public static void main(String args[]){
Path path=Paths.get("c:\\demotext.txt");
try{
UserPrincipal owner=Files.getOwner(path);
System.out.format("The owner of file is: %s%n",owner.getName());
UserPrincipalLookupService lookupservice=FileSystems.getDefault().getUserPrincipalLookupService();
Files.setOwner(path,lookupservice.lookupPrincipalByName("joe"));
UserPrincipal newowner=Files.getOwner(path);
System.out.format("Now the owner of file is: %s%n",newowner.getName());
}
catch(UnsupportedOperationException x){
System.err.println(x);
}
catch(IOException x){
System.err.println(x);
}
}
}
输出:
文件的所有者是:\Everyone
java.nio.file.attribute.UserPrincipalNotFoundException
程序抛出 IOException。这是否意味着我的操作系统限制了对文件所有者的修改?如果没有,请建议我一些解决方案。