jdk7.NIO.2学习笔记之目录文件及权限
package com.zl.jdk7; import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths; public class PathTest1 {
public static void main(String[] args) {
Path listing = Paths.get("/home/c3t/1.js");
//得到文件名
System.out.println("File name :"+listing.getFileName());
//获得名称元素的数量,就是目录的数量
System.out.println("number of name elements :"+listing.getNameCount());
//获得父目录路径
System.out.println("parent path :"+listing.getParent());
//得到根目录
System.out.println("Root path :"+listing.getRoot());
//得到根目录(0)到期第二个元素(2)之间的子目录
System.out.println("Subpath from root :" + listing.subpath(0, 2)); try {
//.vimrc是一个软链接 得到链接的真实文件地址
Path realPath = Paths.get("/home/c3t/.vimrc").toRealPath();
System.out.println("real path:"+realPath.toString());
} catch (final Exception e) {
e.printStackTrace();
} //合并目录,合并后 /home/c3t/conf/application
Path prefix = Paths.get("/home/c3t/");
Path completePath = prefix.resolve("conf/application");
System.out.println("resolve:"+completePath.toString()); //获得两个路径之间的路径 结果得到 从 /usr目录到/home/c3t/waller的路径 relativeize:../home/c3t/waller
String logging ="/usr";
String configuation = "/home/c3t/waller";
Path logdir = Paths.get(logging);
Path confDir = Paths.get(configuation);
Path pathtoConfDir = logdir.relativize(confDir);
System.out.println("relativeize:"+pathtoConfDir.toString()); //NIO.2 PATH和java已有的file类转换
//java.io.File新增了 toPath方法可以把已有的File转化为Path
//Path有toFile可以把path转化为File File file = new File("/home/c3t/1.js");
Path lp = file.toPath();
System.out.println("file to Path:"+lp.toAbsolutePath().toString());
file = lp.toFile();
}
}
//创建文件
Path target = Paths.get("/home/c3t/jdk7.txt");
//设置文件权限 由于目录权限限制 结果可能是 rw-r--r--
Set<PosixFilePermission> perms= PosixFilePermissions.fromString("rw-rw-rw-");
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms); //创建文件
try {
Files.createFile(target,attr);
} catch (IOException e) {
e.printStackTrace();
} //删除文件
try {
Files.delete(target);
} catch (IOException e) {
e.printStackTrace();
}
自定义权限的删除和添加
Path profile = Paths.get("/home/c3t/1.js");
try {
//获得属性视图
PosixFileAttributes attrs = Files.readAttributes(profile, PosixFileAttributes.class);
//得到权限集合
Set<PosixFilePermission> posixFilePermission = attrs.permissions();
//清除所有权限
posixFilePermission.clear();
//得到文件爱你所有者
String owner = attrs.owner().getName();
//得到权限的字符串形式
String perms = PosixFilePermissions.toString(posixFilePermission);
//加入自定义权限
posixFilePermission.add(PosixFilePermission.OWNER_READ);
posixFilePermission.add(PosixFilePermission.OWNER_WRITE);
posixFilePermission.add(PosixFilePermission.OTHERS_READ);
posixFilePermission.add(PosixFilePermission.OTHERS_WRITE);
//写入权限
Files.setPosixFilePermissions(profile,posixFilePermission);
} catch (IOException e) {
e.printStackTrace();
}
//读文件
@Test
public void test2() {
Path profile = Paths.get("/home/c3t/1.js");
try (BufferedReader reader = Files.newBufferedReader(profile, StandardCharsets.UTF_8)) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
//追加写文件
@Test
public void test3() {
Path profile = Paths.get("/home/c3t/1.js");
try (BufferedWriter writer = Files.newBufferedWriter(profile, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
writer.newLine();
writer.write("i am ok");
} catch (IOException e) {
e.printStackTrace();
}
}
jdk7.NIO.2学习笔记之目录文件及权限的更多相关文章
- Windows phone 8 学习笔记(2) 数据文件操作
原文:Windows phone 8 学习笔记(2) 数据文件操作 Windows phone 8 应用用于数据文件存储访问的位置仅仅限于安装文件夹.本地文件夹(独立存储空间).媒体库和SD卡四个地方 ...
- thinkphp学习笔记2—入口文件
原文:thinkphp学习笔记2-入口文件 在thinkphp中有两个入口文件,一个是项目的入口文件,是index.php在主目录里面,还有一个是thinkphp框架的的入口文件,放在框架目录下面如: ...
- thinkphp学习笔记1—目录结构和命名规则
原文:thinkphp学习笔记1-目录结构和命名规则 最近开始学习thinkphp,在下不才,很多的问题看不明白所以想拿出来,恕我大胆发在首页上,希望看到的人能为我答疑解惑,这样大家有个互动,学起来快 ...
- NIO模型学习笔记
NIO模型学习笔记 简介 Non-blocking I/O 或New I/O 自JDK1.4开始使用 应用场景:高并发网络服务器支持 概念理解 模型:对事物共性的抽象 编程模型:对编程共性的抽象 BI ...
- Java学习-042-获取目录文件列表(当前,级联)
以下三个场景,在我们日常的测试开发中经常遇到: 软件自动化测试,在进行参数测试时,我们通常将所有相似功能的参数文件统一放在一个目录中,在自动化程序启动的时候,获取资源参数文件夹中所有参数文件,然后解析 ...
- java之jvm学习笔记三(Class文件检验器)
java之jvm学习笔记三(Class文件检验器) 前面的学习我们知道了class文件被类装载器所装载,但是在装载class文件之前或之后,class文件实际上还需要被校验,这就是今天的学习主题,cl ...
- matlab学习笔记5--低级文件输入输出函数
一起来学matlab-matlab学习笔记5 低级文件输入输出函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用>张德丰等著 感谢张 ...
- [原创]java WEB学习笔记49:文件上传基础,基于表单的文件上传,使用fileuoload 组件
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- Windows phone 8 学习笔记(2) 数据文件操作(转)
Windows phone 8 应用用于数据文件存储访问的位置仅仅限于安装文件夹.本地文件夹(独立存储空间).媒体库和SD卡四个地方.本节主要讲解它们的用法以及相关限制性.另外包括本地数据库的使用方式 ...
随机推荐
- HTML、CSS、JavaScript拾遗
1.html元素中,如果有文本存在,当元素大小不足以容纳文本时,文本会进行强制换行.比如说设置页面不出现滚动条,body的overflow为hidden时,或者scroll为no时,span在超过页面 ...
- PopupWindow简单使用(一)
1.构造函数 //方法一: public PopupWindow (Context context) //方法二: public PopupWindow(View conten ...
- android canvas中rotate()和translate()两个方法详解
rotate()和translate() 1.看到这个题目的时候,有人会觉得这不就是一个对画布的旋转和平移的嘛,但是其中的细节的地方还是需要深究一下的. 例如:有个需求将TextView的文字竖直显示 ...
- GetPixelAddress()函数Alpha通道会丢失
CImage类中GetPixelAddress()函数来设置获取对应的颜色值是发现Alpha无效. void CGBImage::Load(){ CImage sourceImage; sourceI ...
- sql server重建系统数据库
方法一:https://bbs.csdn.net/topics/100013082 方法二:http://blog.51cto.com/jimshu/1095780 *** 方法三:https://b ...
- kali linux之skipfish,arachni
c语言编写,实验性的主动web安全评估工具,递归爬网,基于字典的探测,速度较快--(多路单线程,启发式自动内容识别),误报率低 常用参数 -I 只检查包含/xx/的url -X 不检查包含/xx/的u ...
- Python爬虫:带参url的拼接
如果连接直接这样写,看上去很直观,不过参数替换不是很方便,而且看着不舒服 https://www.mysite.com/?sortField=%E4%BA%BA%E5%B7%A5%E6%99%BA%E ...
- centos7 yum安装配置redis
1.设置Redis的仓库地址 yum install epel-release 2.安装redis yum install redis 修改配置文件,监听所有的IP地址 vim /etc/redis. ...
- count failed: not master{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" } at src/mongo/shell/query.js:191在SECONDARY节点无法show dbs
count failed: not master{ "note" : "from execCommand", "ok" : 0, " ...
- 【python】10分钟教你用python下载和拼接微信好友头像图片
前言 相信微信大家是用得再多也不过了.那么,对于python+微信,又能玩出什么新的花样呢?下面小编就给大家带来一个好玩的东西.用python下载所有的微信好友的头像,然后拼接成一张大图.这样,大家就 ...