Java examples for File Path IO:Unix File
set Posix Owners
/*-/*from w ww. j av a 2 s. c o m*/ * 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.java2s; import java.lang.reflect.Array; 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 .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 if (cause instanceof IllegalArgumentException) { throw (IllegalArgumentException) cause; } else if (cause instanceof InvocationTargetException) { throw (InvocationTargetException) cause; } else if (cause instanceof NoSuchFieldException) { throw (NoSuchFieldException) cause; } else if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } else { throw ex; } } } }