集册 Java实例教程 创建符号链接

创建符号链接

欢马劈雪     最近更新时间:2020-01-02 10:19:05

521
创建一个符号链接

import java.io.IOException;

import java.nio.file.FileSystem;

import java.nio.file.FileSystems;/** 来自 N o w J a v a . c o m - 时  代  Java**/

import java.nio.file.Files;

import java.nio.file.Path;



public class SymbolicLink {

    /*

    Create a symbolic link:

    Windows:

    mklink /D newlinkfolder targetfolder

    linux:

    ln -s targetfolder newlinkfolder

     */

    public static void main(String[] args) {

        Path link = FileSystems.getDefault().getPath("D:/intro/new");

        Path target = FileSystems.getDefault().getPath("C:/Users");


        try {

            if (Files.notExists(link)) {

                Files.createSymbolicLink(link, target);

            }

        } catch (IOException | UnsupportedOperationException e) {

            e.printStackTrace();/**N o  w  J a v a . c o m - 时  代  Java**/

            if (e instanceof UnsupportedOperationException) {

                System.out.println("UnsupportOp!");

            }

        }

        //Checking a symbolic link

        System.out.println(Files.isSymbolicLink(link));


        //Locating the target of a link


        try {

            Path tar = Files.readSymbolicLink(link);

            System.out.println(tar);

        } catch (IOException e) {

            e
展开阅读全文