如何实现“mklink /H” (硬链接)在Java中?

发布于 2024-12-22 05:20:17 字数 627 浏览 2 评论 0原文

我想创建从文件 "C:\xxx.log" 到 "C:\mklink\xxx.log" 的硬链接。 在 cmd 中它当然可以工作,但我想为这个用例编写一个软件。

  • 因此必须找到现有文件
  • 然后创建硬链接
  • 然后删除我开始实现的旧文件

,但是我只知道如何创建文件。在谷歌上我没有找到任何关于 mklink \H for Java 的信息。

public void createFile() {
     boolean flag = false;

     // create File object
     File stockFile = new File("c://mklink/test.txt");

     try {
         flag = stockFile.createNewFile();
     } catch (IOException ioe) {
          System.out.println("Error while Creating File in Java" + ioe);
     }

     System.out.println("stock file" + stockFile.getPath() + " created ");
}

i want to create a hardlink from a file "C:\xxx.log" to "C:\mklink\xxx.log" .
In cmd it works of course, but i want to write a software for this usecase.

  • So have to locate the existing file
  • Then make a hardlink
  • Then delete the old file

I started to implement but, i just know how to create a file. On google i found nothing about mklink \H for Java.

public void createFile() {
     boolean flag = false;

     // create File object
     File stockFile = new File("c://mklink/test.txt");

     try {
         flag = stockFile.createNewFile();
     } catch (IOException ioe) {
          System.out.println("Error while Creating File in Java" + ioe);
     }

     System.out.println("stock file" + stockFile.getPath() + " created ");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖风昔人 2024-12-29 05:20:17

在JAVA中创建硬链接有3种方法。

  1. JAVA 1.7 支持硬链接。

    http://docs.oracle.com/javase /tutorial/essential/io/links.html#hardLink

  2. JNA,JNA 允许您进行本机系统调用。

    https://github.com/twall/jna

  3. JNI,您可以使用C++创建一个硬链接,然后通过JAVA调用。

希望这有帮助。

There are 3 ways to create a hard link in JAVA.

  1. JAVA 1.7 Supports hardlinks.

    http://docs.oracle.com/javase/tutorial/essential/io/links.html#hardLink

  2. JNA, The JNA allows you to make native system calls.

    https://github.com/twall/jna

  3. JNI, you could use C++ to create a hardlink and then call it through JAVA.

Hope this helps.

零度° 2024-12-29 05:20:17

链接(软链接或硬链接)是一种不向标准 java API 公开的操作系统功能。我建议您使用 Runitme.exec()ProcessBuilder 从 java 运行命令 mklink /h

或者尝试找到包装此内容的第 3 方 API。还要检查 Java 7 中的新增功能。不幸的是我不熟悉它,但我知道他们添加了丰富的文件系统 API。

Link (soft or hard) is a OS feature that is not exposed to standard java API. I'd suggest you to run command mklink /h from java using Runitme.exec() or ProcessBuilder.

Or alternatively try to find 3rd party API that wraps this. Also check what's new in Java 7. Unfortunately I am not familiar with it but I know that they added rich file system API.

墨落画卷 2024-12-29 05:20:17

为了后代,我使用以下方法在 *nix/OSX 或 Windows 上创建链接。在 Windows 上,mklink /j 创建一个“连接”,它似乎类似于符号链接。

protected void makeLink(File existingFile, File linkFile) throws IOException {
    Process process;
    String unixLnPath = "/bin/ln";
    if (new File(unixLnPath).canExecute()) {
        process =
                Runtime.getRuntime().exec(
                        new String[] { unixLnPath, "-s", existingFile.getPath(), linkFile.getPath() });
    } else {
        process =
                Runtime.getRuntime().exec(
                        new String[] { "cmd", "/c", "mklink", "/j", linkFile.getPath(), existingFile.getPath() });
    }
    int errorCode;
    try {
        errorCode = process.waitFor();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new IOException("Link operation was interrupted", e);
    }
    if (errorCode != 0) {
        logAndThrow("Could not create symlink from " + linkFile + " to " + existingFile, null);
    }
}

For posterity, I use the following method to create links on *nix/OSX or Windows. On windows mklink /j creates a "junction" which seems to be similar to a symlink.

protected void makeLink(File existingFile, File linkFile) throws IOException {
    Process process;
    String unixLnPath = "/bin/ln";
    if (new File(unixLnPath).canExecute()) {
        process =
                Runtime.getRuntime().exec(
                        new String[] { unixLnPath, "-s", existingFile.getPath(), linkFile.getPath() });
    } else {
        process =
                Runtime.getRuntime().exec(
                        new String[] { "cmd", "/c", "mklink", "/j", linkFile.getPath(), existingFile.getPath() });
    }
    int errorCode;
    try {
        errorCode = process.waitFor();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new IOException("Link operation was interrupted", e);
    }
    if (errorCode != 0) {
        logAndThrow("Could not create symlink from " + linkFile + " to " + existingFile, null);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文