Java UserDefinedFileAttributeView

问题描述 投票:0回答:2

当我向文件添加新的

UserDefinedFileAttributeView
属性时,Java 将该信息存储在哪里? 目录中没有其他文件,当我查看文件属性时,该文件没有任何新属性或详细信息。

我的代码:

String versionAttrName = "report.version";
try {           
    Path path = FileSystems.getDefault().getPath(filePath, "");
    UserDefinedFileAttributeView view = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);
    view.write(versionAttrName, Charset.defaultCharset().encode(newVersion));
} catch (IOException e) {
    System.out.println("7 - Error saving version to file! - "+ filePath + " - " + e.getMessage());
}
java nio xattr
2个回答
4
投票

这些元数据使用文件系统的“扩展文件属性”存储(请参阅wikipedia)。

支持的文件系统列表肯定是特定于 JVM 的,因此您应该在计划支持的所有平台上进行测试。但是this答案列出了一些。

在 Linux 上,您可以使用

getfattr
命令:

查看这些属性
$ getfattr -d test.txt
# file: test.txt
user.Name1="Value1"
user.Name2="Value2"

2
投票

这取决于您运行的平台。

根据 OpenJDK 文档:

The details of such emulation are highly implementation specific and therefore not specified.

但是 Windows 上的测试表明它使用 NTFS 备用数据流:

PS C:\test> Get-Item -Path C:\test\asdf.txt -stream *


   FileName: C:\test\asdf.txt

Stream                   Length
------                   ------
:$DATA                        0
myaltstream                1337

在 Linux 上它使用扩展属性。

不确定其他人 - 希望这有帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.