Close

Java IO & NIO - Files.createSymbolicLink() Examples

Java IO & NIO Java Java API 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.nio.file.FilesLogicBig

Method:

public static Path createSymbolicLink(Path link,
                                      Path target,
                                      FileAttribute<?>... attrs)
                               throws IOException

Creates a symbolic link to a target Path.

Note that the Java code using this method will fail if run without Admin access. To run with Admin access, for example on windows, right click on cmd.exe and select 'Run as administrator' and run the Java program from there.

Parameters:
link - the path of the symbolic link to create
target - the target of the symbolic link
attrs - the array of attributes to set atomically when creating the symbolic link
Returns:
the path to the symbolic link

See Also: Understanding symbolic links and hard links


Examples


package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CreateSymbolicLinkExample {

public static void main(String... args) throws IOException {
//create a new file and write some data
Path dir = Files.createTempDirectory(Paths.get("D:\\"), "my-dir");
Path srcFilePath = dir.resolve("test-file.txt");
Files.write(srcFilePath, "Test Data".getBytes());
//create a symLink
Path symLinkToCreate = dir.resolve("linked-test-file.txt");
Path symLink = Files.createSymbolicLink(symLinkToCreate, srcFilePath);
System.out.println("File sym symLink created: " + symLink);
System.out.println("Regular file: " + Files.isRegularFile(symLink));
System.out.println("Symbolic file: " + Files.isSymbolicLink(symLink));
System.out.println("Same files: " + Files.isSameFile(symLink, srcFilePath));
//reading from symLink

String linkData = new String(Files.readAllBytes(symLink));
System.out.println("Sym Linked file content: " + linkData);
}
}

Output

File sym symLink created: D:\my-dir1368043827292704948\linked-test-file.txt
Regular file: true
Symbolic file: true
Same files: true
Sym Linked file content: Test Data

You must run examples with admin access otherwise you will have following exception

Exception in thread 'main' java.nio.file.FileSystemException: D:\my-dir2474345100059885274\linked-test-file.txt: A required privilege is not held by the client.
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileSystemProvider.createSymbolicLink(WindowsFileSystemProvider.java:585)
at java.nio.file.Files.createSymbolicLink(Files.java:1043)
at com.logicbig.example.files.CreateSymbolicLinkExample.main(CreateSymbolicLinkExample.java:26)




Modifying source file will modify the symbolic link as well

package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class CreateSymbolicLinkExample2 {

public static void main(String... args) throws IOException {
//create a new file and write some data
Path dir = Files.createTempDirectory(Paths.get("D:\\"), "my-dir");
Path srcFilePath = dir.resolve("test-file.txt");
Files.write(srcFilePath, "Test Data".getBytes());
//create a symLink
Path symLinkToCreate = dir.resolve("linked-test-file.txt");
Path symLink = Files.createSymbolicLink(symLinkToCreate, srcFilePath);
//modifying original file
Files.write(srcFilePath, " .. more data ..".getBytes(), StandardOpenOption.APPEND);
//reading sym link
String linkData = new String(Files.readAllBytes(symLink));
System.out.println("Sym Linked file content:\n " + linkData);
}
}

Output

Sym Linked file content:
Test Data .. more data ..




package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class CreateSymbolicLinkExample3 {

public static void main(String... args) throws IOException {
//create a new file and write some data
Path dir = Files.createTempDirectory(Paths.get("D:\\"), "my-dir");
Path srcFilePath = dir.resolve("test-file.txt");
Files.write(srcFilePath, "Test Data".getBytes());
//create a symLink
Path symLinkToCreate = dir.resolve("linked-test-file.txt");
Path symLink = Files.createSymbolicLink(symLinkToCreate, srcFilePath);
//modifying symLink file
Files.write(symLink, " .. more data ..".getBytes(), StandardOpenOption.APPEND);
//reading source file
String linkData = new String(Files.readAllBytes(srcFilePath));
System.out.println("Source file content:\n " + linkData);
}
}

Output

Source file content:
Test Data .. more data ..




Deleting Source file will also delete the Sym link

package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CreateSymbolicLinkExample4 {

public static void main(String... args) throws IOException {
//create a new file and write some data
Path dir = Files.createTempDirectory(Paths.get("D:\\"), "my-dir");
Path srcFilePath = dir.resolve("test-file.txt");
Files.write(srcFilePath, "Test Data".getBytes());
//create a symLink
Path symLinkToCreate = dir.resolve("linked-test-file.txt");
Path symLink = Files.createSymbolicLink(symLinkToCreate, srcFilePath);
//deleting original file
Files.delete(srcFilePath);
System.out.println("Source file exits after delete: " + Files.exists(srcFilePath));
System.out.println("Sym link exits after source file delete: " + Files.exists(symLink));
}
}

Output

Source file exits after delete: false
Sym link exits after source file delete: false




See Also