将自定义属性或元数据添加到文件java

我的文件需要一个名为“加密使用”的额外属性。 但这给出了“IllegalArgumentExeption”。 我知道它为什么会出现这个错误,“使用加密”并不是一个属性,但有没有办法可以强制它呢? 或者将自定义元数据添加到文件中?

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()); } 

如果您的文件系统支持用户定义(又称扩展)属性,那么设置一个属性的方式如下:

 Files.setAttribute(path, "user:encryption used", "testtesttest"); 

正如setAttribute的javadoc所解释的那样,第二个参数采用可选的视图名称和属性名称的forms。 在这种情况下,您需要使用其视图名称为“user”的UserDefinedFileAttributeView

请注意,不同的文件系统类型支持不同的属性视图,您的文件系统可能不支持此视图。