Program to Create Symbolic Link in Java

Below is the code implementation to create a symbolic link to the original file.

Java




// Java program to demonstrate the creation of Symbolic link 
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
  
public class SymbolicLinkExample 
{
    public static void main(String args[]) 
    {
        try {
            // specify the original file and the symbolic link path
            Path originalPath = Paths.get("original.txt");
            Path symbolicLinkPath = Paths.get("symbolic_link.txt");
  
            // create a symbolic link
            Files.createSymbolicLink(symbolicLinkPath, originalPath);
  
            System.out.println("Symbolic link created successfully.");
        } catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}


Output

Symbolic link created successfully.

Explanation of the above Program:

  • In the above program, it creates a symbolic link named symbolic_link.txt that points to the original file original.txt.
  • The createSymbolicLink method is used to create the symbolic link.
  • After that, the path to the original file and the path to the symbolic link to be created.
  • If the symbolic link is created successfully, the output message will be printed.

How to Create Symbolic Links and Hard Links in Java?

Java file system provides both hard and symbolic links, which let us make references to files and directories. In this article, we will learn how to create symbolic links and hard links in Java.

  • Soft Links or Symbolic Links: Symbolic links are names of files or directories. They may stretch across several file systems and serve as pointers to the original file or directory.
  • Hard Links: Multiple directory entries pointing to the same disk are known as hard links. All files that reference the same will update in response to changes made to one file.

Similar Reads

Program to Create Symbolic Link in Java

Below is the code implementation to create a symbolic link to the original file....

Program to Create Hard Links in Java

...