如何实现“mklink /H” (硬链接)在Java中?
我想创建从文件 "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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在JAVA中创建硬链接有3种方法。
JAVA 1.7 支持硬链接。
http://docs.oracle.com/javase /tutorial/essential/io/links.html#hardLink
JNA,JNA 允许您进行本机系统调用。
https://github.com/twall/jna
JNI,您可以使用C++创建一个硬链接,然后通过JAVA调用。
希望这有帮助。
There are 3 ways to create a hard link in JAVA.
JAVA 1.7 Supports hardlinks.
http://docs.oracle.com/javase/tutorial/essential/io/links.html#hardLink
JNA, The JNA allows you to make native system calls.
https://github.com/twall/jna
JNI, you could use C++ to create a hardlink and then call it through JAVA.
Hope this helps.
链接(软链接或硬链接)是一种不向标准 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 usingRunitme.exec()
orProcessBuilder
.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.
为了后代,我使用以下方法在 *nix/OSX 或 Windows 上创建链接。在 Windows 上,
mklink /j
创建一个“连接”,它似乎类似于符号链接。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.