问题
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
viaWatchService
(at least for Linux/Unix filesystems). Therefore I can't use aPath
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