Java7 / Path / File / get a unique ID like an Inode of a file

浪子不回头ぞ 提交于 2019-12-11 05:35:52

问题


How do I get a unique ID from a Path or File instance which must have an Inode or another unique ID in a "non Linux/Unix" filesystem? Can't find anything in the API, do I have to use JNI?

Edit: I have two reasons:

  • I want to have a unique mapping from a file in the filesystem to a tree-based representation in a database system.
  • I want to detect renames of a file, which results in a deletion event and a new insertion event when watching a Path via WatchService (at least for Linux/Unix filesystems). Therefore I can't use a Path to detect such renames.

BTW: At first I thought WatchService would trigger an java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY event in case of renames, but I suppose that's Filesystem dependent and/or only happens if the file content itself changes.

So I suppose I would really have to use a JNI-binding to detect renames and maybe moves?

Edit: I think I've finally found how to get a unique file representation: http://download.oracle.com/javase/7/docs/api/java/nio/file/attribute/BasicFileAttributes.html#fileKey()


回答1:


If all you want is a unique id, you can hash the file path (using sha1) and it will give you an unique text id.

If you want the real id associated with the file, you'll need to use JNI and have a custom library for each OS.




回答2:


A UNC path would be a unique key: "/ / SERVER / PATH / PATH / FILENAME", with an additional File.getCanonicalPath(). At least under Windows as opposed to drive letters.



来源:https://stackoverflow.com/questions/8214576/java7-path-file-get-a-unique-id-like-an-inode-of-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!