用于跟踪Java中文件更改的文件ID? [英] File id for keeping track of file changes in Java?
问题描述
我试图找到一种跟踪文件的方法,即使它们在文件系统中被移动或重命名.
I'm trying to find a way to keep track of files even when they are moved or renamed in the file system.
我想到的一个想法是在Java 7中使用新的UserDefinedFileAttributeView并创建一个自定义文件属性作为一种自定义ID.我认为这可能适用于不同的平台(主要是Windows和Mac).但是我无法使它正常工作.甚至在此页面上尝试该示例时,这行:
One idea I had was to use the new UserDefinedFileAttributeView in Java 7 and create a custom file attribute as a sort of custom id. I thought that this might work on different platforms (Windows and Mac primarily). But I can't get it to work. Even trying the example on this page, when I get to this line:
UserDefinedFileAttributeView view = Files.getFileAttributeView(file, UserDefinedFileAttributeView.class);
我只为view变量获取了一个空值,然后程序由于空指针异常而停止了.
I only get a null value for the view variable, and then the program stops with a nullpointer exception.
然后,我发现至少有一种针对Mac的方法可能更简单:使用BasicFileAttributes fileKey属性.我试过了,即使我移动文件或重命名文件,fileKey似乎也保持不变.但是,它也表示此功能取决于平台,我记得在某处读到它在Windows上不起作用...
I then found that there is a perhaps easier way to do this for Mac at least: use the BasicFileAttributes fileKey attribute. I tried this, and the fileKey seems to stay the same even if I move the file or rename it. However, it also says that this functionality is platform dependent, and I remember reading somewhere that it doesn't work on Windows...
首先,在Mac上,fileKey方法是一种稳定的方法吗?如果是这样,对于Windows的相同功能我该怎么办?有人知道为什么我在UserDefinedFileAttributeView上获取null吗?因为我想,如果我能做到这一点应该是跨平台的.
So first of all, is the fileKey method a stable way of doing this on Mac? And if so, what can I do for the same functionality for Windows? Anyone know why I get null on the UserDefinedFileAttributeView? Because if I could get that to work it should be cross-platform I guess.
这不是需要超级鲁棒和可扩展的东西,它只是我正在开发的一个小助手应用程序,但是在移动或重命名文件时至少必须可靠地标识文件...
This is not something that needs to be super robust and scalable, it is just a small helper application I'm developing, but it needs to be at least reliable in identifying files when moved or renamed...
推荐答案
我在Windows XP计算机上尝试了Oracle示例.该代码示例中有一个非常小的错误,但是除此之外,该代码还可以正常工作-至少在Windows XP上如此.希望它也可以在Linux等系统上运行,但我个人仅在Windows XP上进行过尝试.
I tried the Oracle example on a Windows XP computer. There was a very minor bug in the code example, but other than this, the code worked fine -- at least on Windows XP. Hopefully it would also work on Linux etc, but I personally have only tried it on Windows XP.
public static void main(String args[])
throws Exception
{
Path target = Paths.get("C:\\mytemp\\Something.txt");
Files.createFile(target);
UserDefinedFileAttributeView view = Files.getFileAttributeView(target, UserDefinedFileAttributeView.class);
view.write("user.mimetype", Charset.defaultCharset().encode("text/html"));
String name = "user.mimetype";
ByteBuffer buf = ByteBuffer.allocate(view.size(name));
view.read(name, buf);
buf.flip();
String value = Charset.defaultCharset().decode(buf).toString();
System.out.println("value="+value);
只是确保不只是从视图中读取属性,我还使用第二个视图运行了相同的代码.这也起作用...
Just to be sure the attribute was not just being read from the view, I also ran the same code using a 2nd view. This also worked...
public static void main(String args[])
throws Exception
{
Path target = Paths.get("C:\\mytemp\\SomethingDifferent.txt");
Files.createFile(target);
UserDefinedFileAttributeView view = Files.getFileAttributeView(target, UserDefinedFileAttributeView.class);
view.write("user.mimetype", Charset.defaultCharset().encode("text/html"));
String name = "user.mimetype";
UserDefinedFileAttributeView view2 = Files.getFileAttributeView(target, UserDefinedFileAttributeView.class);
ByteBuffer buf = ByteBuffer.allocate(view2.size(name));
view2.read(name, buf);
buf.flip();
String value = Charset.defaultCharset().decode(buf).toString();
System.out.println("value="+value);
}
如果这样的自定义文件属性在所有主要平台上都可以使用,那就太好了,因为在某些情况下,这样的自定义文件属性非常有用.希望他们能做到.
It would be great if such custom file attributes work across all the major platforms, as such custom file attributes are incredibly handy in some situations. Hope they do.
这篇关于用于跟踪Java中文件更改的文件ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!