我在Linux上,我的Java应用程序不是可移植的.
我正在寻找一种在Java中唯一标识文件的方法.我可以使用statfs系统调用,因为该对(f_fsid, ino)唯一地标识了一个文件(不仅跨文件系统),如下所示:http://man7.org/linux/man-pages/man2/statfs.2.html
问题是如果可以fsid直接从Java中提取,那么我可以避免编写JNI函数吗?
inode可以提取NIO,但fsid怎么样?inode和fsid来自不同的结构,由不同的系统调用...
这个java例子演示了如何获取文件的unix inode号。
import java.nio.file.*;
import java.nio.file.attribute.*;
public class MyFile {
public static void main(String[] args) throws Exception {
BasicFileAttributes attr = null;
Path path = Paths.get("MyFile.java");
attr = Files.readAttributes(path, BasicFileAttributes.class);
Object fileKey = attr.fileKey();
String s = fileKey.toString();
String inode = s.substring(s.indexOf("ino=") + 4, s.indexOf(")"));
System.out.println("Inode: " + inode);
}
}
Run Code Online (Sandbox Code Playgroud)
输出
$ java MyFile
Inode: 664938
$ ls -i MyFile.java
664938 MyFile.java
Run Code Online (Sandbox Code Playgroud)
信用到期的信用: https: //www.javacodex.com/More-Examples/1/8