Create a symbolic link - Java File Path IO

Java examples for File Path IO:Symbolic Link

Description

Create a symbolic link

Demo Code

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;


public class SymbolicLink {
    /*/*  ww  w . ja v a  2 s.  c  o m*/
    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();
            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.printStackTrace();
        }

        //Checking link and target point to the same file
        try {
            System.out.println(Files.isSameFile(link, target));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Related Tutorials