zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    

      

  • 相关阅读:
    (七)android开发中两种方式监听短信的原理和实现
    (三)android中Toast的使用
    (二)、Android ListView滑动过程中图片显示重复错位闪烁问题解决
    (一)PagerAdapter、FragmentPagerAdapter、FragmentStatePagerAdapter的区别
    (六)Android中使用CountDownTimer实现倒计时功能
    (五)在android 4.4上设置手机状态栏的背景
    (四)使用PagerSlidingTabStrip和ViewPager实现可左右滑动和点击效果功能
    devexpress表格gridcontrol实现列统计,总计,平均,求和等。
    常用GDB命令行调试命令
    新浪微博SSO授权后回调客户端没有执行sinaweiboDidLogIn&无法返回应用
  • 原文地址:https://www.cnblogs.com/or2-/p/3653582.html
Copyright © 2011-2022 走看看