Java examples for File Path IO:Symbolic Link
create Symbolic Link
/*-/*www . j a v a 2 s . co 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.InvocationTargetException; import java.lang.reflect.Method; public class Main { public static void createSymbolicLink(String linkPathString, String targetPathString) 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<?> fileAttributeClass = Class .forName("java.nio.file.attribute.FileAttribute"); Object emptyFileAttributeArray = Array.newInstance( fileAttributeClass, 0); Method fileSystemsGetDefaultMethod = fileSystemsClass .getMethod("getDefault"); Method fileSystemGetPathMethod = fileSystemClass.getMethod( "getPath", String.class, String[].class); Method filesCreateSymbolicLinkMethod = filesClass.getMethod( "createSymbolicLink", pathClass, pathClass, emptyFileAttributeArray.getClass()); /* java.nio.file.FileSystem defaultFileSystem = java.nio.file.FileSystems.getDefault(); */ Object defaultFileSystemObject = fileSystemsGetDefaultMethod .invoke(null); /* java.nio.file.Path linkPath = defaultFileSystem.getPath(linkPathString); */ Object linkPathObject = fileSystemGetPathMethod.invoke( defaultFileSystemObject, linkPathString, new String[0]); /* java.nio.file.Path targetPath = defaultFileSystem.getPath(targetPathString); */ Object targetPathObject = fileSystemGetPathMethod.invoke( defaultFileSystemObject, targetPathString, new String[0]); /* java.nio.file.Files.createSymbolicLink(linkPath, targetPath); */ try { filesCreateSymbolicLinkMethod.invoke(null, linkPathObject, targetPathObject, emptyFileAttributeArray); } catch (InvocationTargetException e) { final Throwable cause = e.getCause(); 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 e; } } } }