问题描述
如何从 Path
或 File
实例中获得唯一ID,该实例在非Linux/Unix"文件系统中必须具有Inode或另一个唯一ID?在API中找不到任何内容,我必须使用JNI吗?
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?
我有两个原因:
- 我想要一个从文件系统中的文件到数据库系统中基于树的表示形式的唯一映射.
- 我想检测文件的重命名,这会导致在通过
WatchService
观看Path
时导致删除事件和新的插入事件(至少对于Linux/Unix是这样)文件系统).因此,我无法使用Path
来检测此类重命名.
- 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:起初我以为 WatchService
在重命名的情况下会触发 java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY
事件,但我想这取决于文件系统和/或仅在文件内容本身发生更改时发生.
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.
所以我想我真的必须使用JNI绑定来检测重命名或移动吗?
So I suppose I would really have to use a JNI-binding to detect renames and maybe moves?
我想我终于找到了如何获得唯一文件表示形式的方法: http://download.oracle.com/javase/7/docs/api/java/nio/file/attribute/BasicFileAttributes.html#fileKey ()
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()
推荐答案
如果您想要的只是一个唯一的ID,则可以对文件路径进行哈希处理(使用sha1),它将为您提供唯一的文本ID .
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.
如果您想要与文件关联的真实ID,则需要使用JNI并为每个操作系统都有一个自定义库.
If you want the real id associated with the file, you'll need to use JNI and have a custom library for each OS.
这篇关于Java7/路径/文件/获得唯一的ID,例如文件的Inode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!