问题描述
我的文件需要一个名为加密使用的额外属性。但这给出了IllegalArgumentExeption。我知道它为什么会出现这个错误,使用加密并不是一个属性,但有没有办法可以强制它呢?或者将自定义元数据添加到文件中?
I have files that need an extra attribute called "encryption used". But this gives "IllegalArgumentExeption". I know why it gives that error, "encryption used" isn't known as an attribute, but is there a way I can force it to be? Or add custom metadata to the file?
Path path = new File("/propertyfiles/encdec.properties").toPath();
try{
Files.setAttribute(path, "encryption used", "testtesttest");
}catch(IOException e){
System.out.println(e.getMessage());
}
try{
System.out.println(Files.getAttribute(path, "encryption used"));
}catch(IOException e){
System.out.println(e.getMessage());
}
推荐答案
如果您的文件系统支持用户-defined(又名扩展)属性,然后设置一个属性的方式如下:
If your file system supports user-defined (aka extended) attributes, then the way to set one would be like this:
Files.setAttribute(path, "user:encryption used", "testtesttest");
作为 setAttribute
解释,第二个参数采用可选视图名称的形式,属性名称。在这种情况下,您需要使用,其视图名称为user。
As the javadoc for setAttribute
explains, the 2nd argument takes the form of an optional view-name and an attribute name. In this case, you need to use the UserDefinedFileAttributeView
whose view-name is "user".
请注意,不同的文件系统类型支持不同属性视图,您的文件系统可能不支持此视图。
Note that different file system types support different attribute views, and your file system may not support this one.
这篇关于将自定义属性或元数据添加到文件java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!