java中setattribute_Java Files.setAttribute方法代码示例

import java.nio.file.Files; //导入方法依赖的package包/类

@Override

public void setFileHiddenAttribute(

String sourceFile,

boolean hidden ) {

sourceFile = IoUtils.normalizeFilePath(sourceFile, osType);

checkFileExistence(new File(sourceFile));

final String errMsg = "Could not " + (hidden

? "set"

: "unset")

+ " the hidden attribute of file '" + sourceFile + "'";

if (OperatingSystemType.getCurrentOsType().isWindows()) {

try {

Path path = Paths.get(sourceFile);

DosFileAttributes attr;

attr = Files.readAttributes(path, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);

boolean goHidden = attr.isHidden();

if (!hidden && goHidden) {

Files.setAttribute(path, "dos:hidden", false, LinkOption.NOFOLLOW_LINKS);

} else if (hidden && !goHidden) {

Files.setAttribute(path, "dos:hidden", true, LinkOption.NOFOLLOW_LINKS);

}

} catch (IOException e) {

throw new FileSystemOperationException(errMsg, e);

}

} else if (OperatingSystemType.getCurrentOsType().isUnix()) {

// a '.' prefix makes the file hidden

String filePath = IoUtils.getFilePath(sourceFile);

String fileName = IoUtils.getFileName(sourceFile);

if (hidden) {

if (fileName.startsWith(".")) {

log.warn("File '" + sourceFile + "' is already hidden. No changes are made!");

return;

} else {

fileName = "." + fileName;

}

} else {

if (!fileName.startsWith(".")) {

log.warn("File '" + sourceFile + "' is already NOT hidden. No changes are made!");

return;

} else {

fileName = fileName.substring(1);

}

}

renameFile(sourceFile, filePath + fileName, false);

} else {

throw new FileSystemOperationException(errMsg + ": Unknown OS type");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`java.nio.file.attribute.UserPrincipalNotFoundException` 是一个检查异常,表示试图访问文件系统不存在的用户或组。 当使用 `Files.setAttribute()` 方法更新文件系统属性时,如果指定的用户或组不存在,则该异常将被抛出。 以下是一个示例代码,演示了如何使用 `Files.setAttribute()` 方法并捕获 `UserPrincipalNotFoundException` 异常: ```java import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.UserPrincipalNotFoundException; import java.nio.file.attribute.UserPrincipal; import java.io.IOException; public class Main { public static void main(String[] args) { Path path = Paths.get("file.txt"); try { UserPrincipal owner = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("johndoe"); Files.setAttribute(path, "owner:owner", owner); } catch (UserPrincipalNotFoundException e) { System.err.println("User does not exist: " + e.getMessage()); } catch (IOException e) { System.err.println("Error updating file attributes: " + e.getMessage()); } } } ``` 在上面的代码,我们尝试将文件 `file.txt` 的所有权更改为 `johndoe` 用户。如果用户不存在,将捕获 `UserPrincipalNotFoundException` 异常并输出错误消息。如果在更新文件属性时出现任何其他错误,则将捕获 `IOException` 异常并输出错误消息。 注意,在使用 `Files.setAttribute()` 方法之前,必须使用 `UserPrincipalLookupService.lookupPrincipalByName()` 方法查找用户或组的 `UserPrincipal` 对象。如果找不到该用户或组,则将抛出 `UserPrincipalNotFoundException` 异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值