Java IO & NIO - Files.createLink() Examples

Code Snippets Java IO & NIO Java 


Class:

java.nio.file.Files

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

Method:

public static Path createLink(Path link,
                              Path existing)
                       throws IOException

Creates a new link for an existing path.

The type of link is created is dependent on the O.S. Most of the system it will create a "hard link".

Parameters:
link - the link (directory entry) to create
existing - a path to an existing file
Returns:
the path to the link (directory entry)

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;

public class CreateLinkExample {

public static void main(String... args) throws IOException {
//create a new file and write some data
Path dir = Files.createTempDirectory("my-dir");
Path srcFilePath = dir.resolve("test-file.txt");
Files.write(srcFilePath, "Test Data".getBytes());
//create a link
Path linkPath = dir.resolve("linked-test-file.txt");
Path link = Files.createLink(linkPath, srcFilePath);
System.out.println("File link created: " + link);
System.out.println("Regular file: " + Files.isRegularFile(link));
System.out.println("Symbolic file: " + Files.isSymbolicLink(link));
System.out.println("Same files: " + Files.isSameFile(link, srcFilePath));
//reading from link
String linkData = new String(Files.readAllBytes(link));
System.out.println("Linked file content: " + linkData);
}
}

Output

File link created: C:\Users\Joe\AppData\Local\Temp\my-dir9477477621523711070\linked-test-file.txt
Regular file: true
Symbolic file: false
Same files: true
Linked file content: Test Data




Modifying source file will modifying the linked file 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.StandardOpenOption;

public class CreateLinkExample2 {

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

Output

Linked file content after modifying original file:
Test Data .. more data ...




Modifying linked file will modifying the source file 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.StandardOpenOption;

public class CreateLinkExample3 {

public static void main(String... args) throws IOException {
//create a new file and write some data
Path dir = Files.createTempDirectory("my-dir");
Path srcFilePath = dir.resolve("test-file.txt");
Files.write(srcFilePath, "Test Data".getBytes());
//create a link
Path linkPath = dir.resolve("linked-test-file.txt");
Path link = Files.createLink(linkPath, srcFilePath);
//modifying original file
Files.write(link, " .. more data ...".getBytes(), StandardOpenOption.APPEND);
String srcData = new String(Files.readAllBytes(srcFilePath));
System.out.println("Source file content after modifying linked file:\n" + srcData);
}
}

Output

Source file content after modifying linked file:
Test Data .. more data ...




Deleting source file will not affect linked file and vice versa.

package com.logicbig.example.files;

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

public class CreateLinkExample4 {

public static void main(String... args) throws IOException {
//create a new file and write some data
Path dir = Files.createTempDirectory("my-dir");
Path srcFilePath = dir.resolve("test-file.txt");
Files.write(srcFilePath, "Test Data".getBytes());
//create a link
Path linkPath = dir.resolve("linked-test-file.txt");
Path link = Files.createLink(linkPath, srcFilePath);
//deleting original file
Files.delete(srcFilePath);
System.out.println("Source file exits: " + Files.exists(srcFilePath));
System.out.println("linked file exits: " + Files.exists(link));

//reading linked file
String srcData = new String(Files.readAllBytes(link));
System.out.println("Linked file content after deleting source file:\n" + srcData);
}
}

Output

Source file exits: false
linked file exits: true
Linked file content after deleting source file:
Test Data




See Also