java 元数据,使用java获取文件所有者元数据信息

I am trying to retrieve the owner of a file, using this code:

Path file = Paths.get( fileToExtract.getAbsolutePath() );

PosixFileAttributes attr = Files.readAttributes(file, PosixFileAttributes.class); //line that throws exception

System.out.println(attr.owner.getName());

but i always get a UnsupportedOperationException at the line i indicate above.

java.lang.UnsupportedOperationException

at sun.nio.fs.WindowsFileSystemProvider.readAttributes(WindowsFileSystemProvider.java:192)

at java.nio.file.Files.readAttributes(Files.java:1684)

I think that 'readAttributes' method is abstract and this cause the exception, but (if this is true) i don't know how to implement this method in order to give me the file attributes.

Does anyone know how to implement this method, or an alternative method (that is tested) to get the file owner?

解决方案

Try this - works also on Windows

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.attribute.FileOwnerAttributeView;

import java.nio.file.attribute.UserPrincipal;

public class FileOwner {

public static void main(String[] args) throws IOException {

Path path = Paths.get("/tmp");

FileOwnerAttributeView ownerAttributeView = Files.getFileAttributeView(path, FileOwnerAttributeView.class);

UserPrincipal owner = ownerAttributeView.getOwner();

System.out.println("owner: " + owner.getName());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取文件元数据,可以使用Java中的java.nio.file.Files类和java.nio.file.attribute.BasicFileAttributes接口。下面是一个示例代码,可以获取文件所有者和其他元数据信息: ``` import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.FileOwnerAttributeView; import java.nio.file.attribute.UserPrincipal; public class FileMetadataExample { public static void main(String[] args) throws Exception { Path path = Paths.get("path/to/file"); BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class); FileOwnerAttributeView ownerAttributeView = Files.getFileAttributeView(path, FileOwnerAttributeView.class); UserPrincipal owner = ownerAttributeView.getOwner(); System.out.println("Owner: " + owner.getName()); System.out.println("Creation Time: " + attributes.creationTime()); System.out.println("Last Accessed Time: " + attributes.lastAccessTime()); System.out.println("Last Modified Time: " + attributes.lastModifiedTime()); System.out.println("Is Directory: " + attributes.isDirectory()); System.out.println("Is Regular File: " + attributes.isRegularFile()); System.out.println("Is Symbolic Link: " + attributes.isSymbolicLink()); System.out.println("Size: " + attributes.size()); } } ``` 在上面的代码中,我们首先使用Paths.get()方法创建Path对象,然后使用Files.readAttributes()方法和BasicFileAttributes.class参数来读取文件的基本属性。接下来,我们使用Files.getFileAttributeView()方法和FileOwnerAttributeView.class参数来获取文件所有者属性视图,然后使用getOwner()方法获取文件所有者信息。最后,我们输出获取到的所有元数据信息。 注意:获取文件元数据需要访问文件系统,有可能会抛出IOException异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值