[NIO.2] 第十三篇 属性视图之 DOS View

为了支持 DOS(或 Samba) 文件系统,DosFileAttributeView 属性视图扩展了 Basic 属性视图(意味着 DOS 属性视图是 Basic 属性视图的子类,可以直接访问父类的属性)。DOS 属性视图提供了四个属性,对应下面的四个方法:
isReadOnly(): 返回只读属性值(true 表示文件不能被删除或修改)
isHidden(): 返回文件是否隐藏的属性(true 表示文件是隐藏文件)
isArchive(): 返回文件是否为存档文件的属性(用于备份程序)
isSystem(): 返回文件的系统属性(true 表示文件是系统文件)

下面的例子通过演示了如何获取这四个属性值:

import java.io.IOException; 
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.DosFileAttributes;
...
DosFileAttributes attr = null;
Path path = Paths.get("C:/rafaelnadal/tournaments/2009", "BNP.txt");

try {
attr = Files.readAttributes(path, DosFileAttributes.class);
} catch (IOException e) {
System.err.println(e);
}

System.out.println("Is read only ? " + attr.isReadOnly());
System.out.println("Is Hidden ? " + attr.isHidden());
System.out.println("Is archive ? " + attr.isArchive());
System.out.println("Is system ? " + attr.isSystem());


设置和获取单个属性可以使用 setAttribute() 和 getAttribute() 方法,看看下面的例子:

import static java.nio.file.LinkOption.NOFOLLOW_LINKS; 

//setting the hidden attribute to true
try {
Files.setAttribute(path, "dos:hidden", true, NOFOLLOW_LINKS);
} catch (IOException e) {
System.err.println(e);
}

//getting the hidden attribute
try {
boolean hidden = (Boolean) Files.getAttribute(path, "dos:hidden", NOFOLLOW_LINKS);
System.out.println("Is hidden ? " + hidden);
} catch (IOException e) {
System.err.println(e);
}


DOS 属性视图支持以下属性名称:

[list]
[*]hidden
[*]readonly
[*]system
[*]archive
[/list]

访问属性的通用结构是 [view-name:]attribute-name,在这个例子中 view-name 是 dos,attribute-name 是 hidden。

文章来源:[url]http://www.aptusource.org/2014/03/nio-2-dos-view/[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值