zoukankan      html  css  js  c++  java
  • Java Path与Files

    Path和Files类封装了在用户机器上处理文件系统所需的所有功能,Path和Files是在Java SE 7 中新添加进来的类,使用时注意JDK版本。

            //在传统java.io中, 文件和目录都被抽象成File对象, 即 File file = new File(".");
            //NIO.中则引入接口Path代表与平台无关的路径,文件和目录都用Path对象表示
            //通过路径工具类Paths返回一个路径对象Path
            Path path = Paths.get("D:\WorkFiles\Project\chitic-supplywater\chitic-supplywater-common\chitic-supplywater-common-api\src\main\java\com\chitic\supplywater\common\api\util\DateUtil.java");
            //一次性读取
            List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
            StringBuilder sb = new StringBuilder();
            for(String line : lines){
                sb.append(line);
            }
            String fromFile = sb.toString();
            System.out.println(fromFile);
         //一行一行读取文件
            Files.lines(path, StandardCharsets.UTF_8).forEach(System.err::println);
    Path

    Path表示的是一个目录名序列,其后还可以跟着一个文件名。
    注:文件系统的分隔符(类Unix文件系统是 / ,Windows是 )
    路径中的第一个部件可以是根部件,例如 / 或 C: 。以根部件开始的是绝对路径;否则就是相对路径。
    方法:

    static Path get(String first, String ... more)
    通过连接给定的字符串创建一个路径。
    Path absolute = Paths.get("/Users/lujiafeng/Desktop/SpringBoot-Learning/Java_Test/src/cc.imi"); //绝对路径
    Path relative = Paths.get("src/cc.imi");  //相对路径

    注:在project中,相对路径的根目录是project的根文件夹

    Path getParent()
    返回父路径,或者在该路径没有父路径时,返回null。
    Path getFileName()
    返回该路径的最后一个部件,或者在该路径没有任何部件时,返回null。
    Path getRoot()
    返回该路径的根部件,或者在该路径没有任何根部件时,返回null。
    读写文件
    static byte[] readAllBytes(Path path)
    读入文件内容。
    byte[] bytes = Files.readAllBytes(path);
    //将文件当做字符串读入,可添加如下代码:
    String content = new String(bytes, charset);
    static List<String> readAllLines(Path path, Charset charset)
    将文件当做行序列读入。
    List<String> lines = Files.readAllLines(path, charset);
    static Path write(Path path, byte[] contents, OpenOption...options)
    写出一个字符串到文件中。
    Files.write(path, content.getBytes(charset));
    向指定文件中追加内容:
    
    Files.write(path, content.getBytes(charset), StandardOpenOption.APPEND);
    static Path write(Path path, Iterable<? extends CharSequence> contents, OpenOption options)
    将一个行的集合写出到文件中:
    Files.write(path, lines);

    注:以上方法适用于处理中等长度的文本文件,如果要处理的文件长度比较大,或者是二进制文件,那么还是应该使用流或者杜读入器 / 写出器。

    复制、移动、删除文件
    static Path copy(Path from, Path to, CopyOption... options)
    将文件从一个位置复制到另一个位置,并返回to。
    Files.copy(fromPath, toPath);
    static Path move(Path from, Path to, CopyOption... options)
    将文件从一个位置移动到另一个位置,并返回to。如果目标路径已经存在,那么复制或移动将失败。
    Files.move(fromPath, toPath);
    static void delete(Path path)
    删除给定文件或空目录,在文件或目录不存在的情况下抛出异常。
    static boolean deleteIfExists(Path path)
    删除给定文件或空目录。在文件或目录不存在的情况下返回false。
    创建文件和目录
    创建新目录:
    Files.createDirectory(path);
    创建一个空文件
    Files.createFile(path);
    获取文件信息
    • static boolean exists(Path path)
    • static boolean isHidden(Path path)
    • static boolean isReadable(Path path)
    • static boolean isWriter(Path path)
    • static boolean isExecutable(Path path)

    https://blog.csdn.net/oMaoYanEr/article/details/80961130

  • 相关阅读:
    python 并发编程 多线程 event
    python 并发编程 多线程 定时器
    python 并发编程 多线程 信号量
    linux top 查看CPU命令
    python 并发编程 多线程 GIL与多线程
    python 并发编程 多线程 死锁现象与递归锁
    python 并发编程 多线程 GIL与Lock
    python GIL全局解释器锁与互斥锁 目录
    python 并发编程 多线程 GIL全局解释器锁基本概念
    执行python程序 出现三部曲
  • 原文地址:https://www.cnblogs.com/gaomanito/p/11642918.html
Copyright © 2011-2022 走看看