将自定义属性或元数据添加到文件 java [英] add custom attribute or metadata to file java

查看:28
本文介绍了将自定义属性或元数据添加到文件 java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

如果你的文件系统支持用户定义(又名扩展)属性,那么设置一个的方法是这样的:

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");

作为 javadoc for setAttribute 解释,第二参数采用可选的 view-name 和属性名称的形式.在这种情况下,您需要使用 UserDefinedFileAttributeView 其视图名称为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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆