设置Posix所有者
/*- * Copyright (C) 2014 Erik Larsson * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ //package com.nowjava; import java.lang.reflect.Array; /* 时代Java */ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { public static void setPosixOwners(String path, int ownerId, int groupId) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException { Class<?> fileSystemsClass = Class /** from * 时 代 J a v a 公 众 号 - N o w J a v a . c o m **/ .forName("java.nio.file.FileSystems"); Class<?> fileSystemClass = Class .forName("java.nio.file.FileSystem"); Class<?> pathClass = Class.forName("java.nio.file.Path"); Class<?> filesClass = Class.forName("java.nio.file.Files"); Class<?> linkOptionClass = Class .forName("java.nio.file.LinkOption"); Method fileSystemsGetDefaultMethod = fileSystemsClass .getMethod("getDefault"); Method fileSystemGetPathMethod = fileSystemClass.getMethod( "getPath", String.class, String[].class); Field noFollowLinksField = linkOptionClass .getField("NOFOLLOW_LINKS"); Object noFollowLinksObject = noFollowLinksField.get(null); Object linkOptionsArray = Array.newInstance(linkOptionClass, 1); Array.set(linkOptionsArray, 0, noFollowLinksObject); /* java.nio.file.FileSystem defaultFileSystem = java.nio.file.FileSystems.getDefault(); */ Object defaultFileSystemObject = fileSystemsGetDefaultMethod .invoke(null); /* java.nio.file.Path p = defaultFileSystem.getPath(path); */ Object pObject = fileSystemGetPathMethod.invoke( defaultFileSystemObject, path, new String[0]); Method filesSetAttributeMethod = filesClass.getMethod( "setAttribute", pathClass, String.class, Object.class, linkOptionsArray.getClass()); try { /* java.nio.file.Files.setAttribute(p, "unix:uid", Integer.valueOf(ownerId), java.nio.file.LinkOption.NOFOLLOW_LINKS);*/ filesSetAttributeMethod.invoke(null, pObject, "unix:uid", Integer.valueOf(ownerId), linkOptionsArray); /* java.nio.file.Files.setAttribute(p, "unix:gid", Integer.valueOf(groupId), java.nio.file.LinkOption.NOFOLLOW_LINKS); */ filesSetAttributeMethod.invoke(null, pObject, "unix:gid", Integer.valueOf(groupId), linkOptionsArray); } catch (InvocationTargetException ex) { final Throwable cause = ex.getCause(); if (cause instanceof UnsupportedOperationException) { /* Do nothing. This is expected on non-UNIX platforms. */ } else if (cause instanceof ClassNotFoundException) { throw (ClassNotFoundException) cause; } else if (cause instanceof NoSuchMethodException) { throw (NoSuchMethodException) cause; } else if (cause instanceof IllegalAccessException) { throw (IllegalAccessException) cause; } else