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

use*_*531 6 java io metadata file

我的文件需要一个名为"加密使用"的额外属性.但是这给出了"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());
    }
Run Code Online (Sandbox Code Playgroud)

Ste*_*n C 7

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

Files.setAttribute(path, "user:encryption used", "testtesttest");
Run Code Online (Sandbox Code Playgroud)

作为javadoc的用于setAttribute解释的那样,第二参数采用一个可选的形式视图名称和属性名称.在这种情况下,您需要使用UserDefinedFileAttributeView其视图名称为"user"的用户.

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