集册 Java实例教程 创建硬链接

创建硬链接

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

477
以下代码创建一个硬链接。

import java.io.IOException;

import java.nio.file.FileSystems;

import java.nio.file.Files;/*来自 N o w  J a v a  .   c o m*/

import java.nio.file.Path;


public class Main {

  public static void main(String[] args) {

    Path link = FileSystems.getDefault().getPath("test");

    Path target = FileSystems.getDefault().getPath("C:/folder1/photos",

        "test.jpg");

    try {

      Files.createLink(link, target);

      System.out.println("The link was successfully created!");

    } catch (IOException | UnsupportedOperationException | SecurityException e) {

      if (e instanceof SecurityException) {

        System.err.println("Permission denied!");

      }

      if (e instanceof UnsupportedOperationException) {

        System.err.println("An unsupported operation was detected!");//来自 N o w  J a v
展开阅读全文