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. Promise超时情况

    export const ERROR_PROMISE_TIMEOUT = 'ERROR_PROMISE_TIMEOUT'; export default function (promise, time ...

  2. WeStrom自定义设置修改快捷键

    按照下图操作,不BB: 终极懒人设置:!!!

  3. 三、Node.js-HelloWorld案例

    之前我们编写的JavaScript代码都是在浏览器中运行的,因此,我们可以直接在浏览器中敲代码,然后直接运行. 在Node,我们编写的JavaScript代码将不能在浏览器环境中执行了,而是在Node ...

  4. openvpn的搭建与应用

    一.VPN概述: VPN(Virtual Private NetWork,虚拟专用网络)架设在公共共享的基础设施互联网上,在非信任的网络上建立私有的安全的连接,把分布在不同地域的办公场所.用户或者商业 ...

  5. Delphi XE8中开发DataSnap程序常见问题和解决方法 (-)启动创建好的DBExpress工程时候报错了!

    当我们成功创建了使用DBExpress的DataSnap的服务器和客户端程序后,我们关闭了当前工程,当我们再次打开时候,有可能会出现这样的问题: 问题原因:这个问题是因为当前工程组默认启动的是客户端工 ...

  6. activeMq延迟消息队列

    Long delay = 30 * 1000L; jmsTemplate.send(type.getValue(),new MessageCreator() { @Override public Me ...

  7. golang并发练习代码笔记

    golang语言的精髓就是它的并发机制,十分简单,并且极少数在语言层面实现并发机制的语言,golang被成为网络时代的c语言,golang的缔造者也有c语言的缔造者,Go语言是google 推出的一门 ...

  8. 魔方方法之--类的构造(__init__,__new__)和析构(__del__)方法

    构造方法(参见小甲鱼入门教程) __ init__()方法:类的初始化方法,初始化类对象时被调用,需要的时候再调用它 注意点:这个方法的返回值必须是None class Rectangle(): de ...

  9. 4、numpy+pandas速查手册

    <Python数据分析常用手册>一.NumPy和Pandas篇 一.常用链接: 1.Python官网:https://www.python.org/2.各种库的whl离线安装包:http: ...

  10. C# 获取类中属性注释值

    转 http://bbs.csdn.net/topics/350019800 PropertyInfo[] peroperties = typeof(TEST).GetProperties(Bindi ...