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学习笔记之目录文件及权限的更多相关文章

  1. Windows phone 8 学习笔记(2) 数据文件操作

    原文:Windows phone 8 学习笔记(2) 数据文件操作 Windows phone 8 应用用于数据文件存储访问的位置仅仅限于安装文件夹.本地文件夹(独立存储空间).媒体库和SD卡四个地方 ...

  2. thinkphp学习笔记2—入口文件

    原文:thinkphp学习笔记2-入口文件 在thinkphp中有两个入口文件,一个是项目的入口文件,是index.php在主目录里面,还有一个是thinkphp框架的的入口文件,放在框架目录下面如: ...

  3. thinkphp学习笔记1—目录结构和命名规则

    原文:thinkphp学习笔记1-目录结构和命名规则 最近开始学习thinkphp,在下不才,很多的问题看不明白所以想拿出来,恕我大胆发在首页上,希望看到的人能为我答疑解惑,这样大家有个互动,学起来快 ...

  4. NIO模型学习笔记

    NIO模型学习笔记 简介 Non-blocking I/O 或New I/O 自JDK1.4开始使用 应用场景:高并发网络服务器支持 概念理解 模型:对事物共性的抽象 编程模型:对编程共性的抽象 BI ...

  5. Java学习-042-获取目录文件列表(当前,级联)

    以下三个场景,在我们日常的测试开发中经常遇到: 软件自动化测试,在进行参数测试时,我们通常将所有相似功能的参数文件统一放在一个目录中,在自动化程序启动的时候,获取资源参数文件夹中所有参数文件,然后解析 ...

  6. java之jvm学习笔记三(Class文件检验器)

    java之jvm学习笔记三(Class文件检验器) 前面的学习我们知道了class文件被类装载器所装载,但是在装载class文件之前或之后,class文件实际上还需要被校验,这就是今天的学习主题,cl ...

  7. matlab学习笔记5--低级文件输入输出函数

    一起来学matlab-matlab学习笔记5 低级文件输入输出函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用>张德丰等著 感谢张 ...

  8. [原创]java WEB学习笔记49:文件上传基础,基于表单的文件上传,使用fileuoload 组件

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  9. Windows phone 8 学习笔记(2) 数据文件操作(转)

    Windows phone 8 应用用于数据文件存储访问的位置仅仅限于安装文件夹.本地文件夹(独立存储空间).媒体库和SD卡四个地方.本节主要讲解它们的用法以及相关限制性.另外包括本地数据库的使用方式 ...

随机推荐

  1. C# Socket 发送&接收&返回

    因为工作比较忙,好久没有写过博客了,最近因项目需求,需要用到Socket来进行通信,简单写了几个例子,记录一下,代码很简单,无非就是接收与发送,以及接收到数据后返回一个自定义信息,也可以说是发送. 先 ...

  2. 深入解析mapreduce 笔记

    Hadoop组成: mapreduce简单介绍: 伪代码演示: maptask和reducetask执行流程:

  3. C语言#include的用法

    1.#include 命令介绍 #include 命令是预处理命令的一种,预处理命令可以将别的源代码内容插入到所指定的位置:可以标识出只有在特定条件下才会被编译的某一段程序代码: 可以定义类似标识符功 ...

  4. ajax的get,post ,封装

    let ajax = new Object(); ajax.get = function(url,fn){ //创建ajax对象 let xhr = new XMLHttpRequest(); //与 ...

  5. django开发日志配置

    做django开发离不开 日志,这用于保存我门的服务器的日志信息,便于开发人员的维护. 直接上代码: 在setting.py文件里直接配置即可 LOGGING = { 'version': 1, 'd ...

  6. linux vmalloc和kmalloc

    kmalloc是内核低端内存的分配,而vmalloc对应内核高端内存的分配.kmalloc()分配的内存处于3GB-high_memory之间,这一段内核空间与物理内存的映射. kmalloc保证分配 ...

  7. cnd 计费流量查询服务模块设计与实现

    一.cdn模块结构: 2.内部模块结构:

  8. linux 下PATH环境变量

    环境变量简介 什么是环境变量呢?简要的说,就是指定一个目录,运行软件的时候,相关的程序将会按照该目录寻找相关文件. 在linux系统下,如果你下载并安装了应用程序,很有可能在键入它的名称时出现&quo ...

  9. 文件句柄NSFileHandle

    //一.读取 //1.以只读方式打开 NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:txtPath]; //2.读取所有内容 ...

  10. springcloud微服务总结五 服务熔断

    一:雪崩效应 如下图所示:A作为服务提供者,B为A的服务消费者,C和D是B的服务消费者.A不可用引起了B的不可用,并将不可用像滚雪球一样放大到C和D时,导致整个系统瘫痪,雪崩效应就形成了. 雪崩过程: ...