美文网首页
Java NIO 的 Files Path 和 Paths

Java NIO 的 Files Path 和 Paths

作者: 华健课堂 | 来源:发表于2024-09-03 10:53 被阅读0次

    小文同学,一目千行看完 java.nio.file package 后,颇有感慨,写下鲁迅千古名句:“希望是本无所谓有,无所谓无的。这正如地上的路;其实地上本没有路,走的人多了,也便成了路。”

    📝 写码时刻到了!📝

    一. Paths 创建文件或目录 Path 路径的工具类

    import java.nio.file.Path;
    import java.nio.file.Paths;
    
    public class Demo {
      public static void main(String[] args) {
      
        // 进程当前工作目录 current working directory
        String pwd = System.getProperty("user.dir");
        System.out.println(pwd);
    
    // 1. get()方法:获取文件或目录的 Path,兼容路径分隔符 / \
    
        // 相对路径 都是相对 进程工作目录
        // 在 IDEA 中运行默认是项目目录 例如 C:\Users\zhouhuajian\Desktop\demo
    
        // 路径 aaa -> C:\Users\zhouhuajian\Desktop\demo\aaa
        Path path = Paths.get("aaa");
        System.out.println(path.toAbsolutePath());
    
        // 路径 aaa\bbb\ccc -> C:\Users\zhouhuajian\Desktop\demo\aaa\bbb\ccc
        Path path2 = Paths.get("aaa\\bbb\\ccc");
        Path path3 = Paths.get("aaa", "bbb", "ccc");
        System.out.println(path2.toAbsolutePath());
        System.out.println(path3.toAbsolutePath());
    
        // 绝对路径
        // 路径 C:\Users\zhouhuajian\Desktop\demo\aaa\bbb\ccc
        Path path4 = Paths.get("C:\\Users\\zhouhuajian\\Desktop\\demo\\aaa\\bbb\\ccc");
        System.out.println(path4.toAbsolutePath());
    
    // 2. 其他方法 ……
    
      }
    }
    /*
    输出
    C:\Users\zhouhuajian\Desktop\demo\aaa
    C:\Users\zhouhuajian\Desktop\demo\aaa\bbb\ccc
    C:\Users\zhouhuajian\Desktop\demo\aaa\bbb\ccc
    C:\Users\zhouhuajian\Desktop\demo\aaa\bbb\ccc
    */
    

    二. Path 表示文件或目录的路径

    import java.nio.file.Path;
    
    public class Demo {
      public static void main(String[] args) {
    
    // 1. of()方法:跟 Paths.get() 用法一样,官方推荐用 Path.of(),并标明 Paths.get() 以后可能会被废弃
    // 2. toAbsolutePath()方法:转成绝对路径
    
        Path path = Path.of("aaa/bbb/ccc");
        System.out.println(path.toAbsolutePath());
    
    // 3. isAbsolute()方法:判断是否是绝对路径
    
        boolean isAbsolute = path.isAbsolute();
        System.out.println(isAbsolute);
    
    // 4. getFileName()方法:获取文件或目录名
    
        String fileName = path.getFileName().toString();
        System.out.println(fileName);
    
    // 5. getParent()方法:获取父级路径/上层路径
    
        String parent = path.getParent().toString();
        System.out.println(parent);
    
    // 6. getRoot()方法:获取根路径
    // 7. toFile()方法:转成 java.io.File 对象
    // 8. startsWith()方法:路径是否以……开头
    // 9. endsWith()方法:路径是否以……结尾
    // 10. 其他方法 ……
    
      }
    }
    /*
    输出
    C:\Users\zhouhuajian\Desktop\demo\aaa\bbb\ccc
    false
    ccc
    aaa\bbb
     */
    

    三. Files 操作文件或目录的工具类

    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.util.List;
    
    public class Demo {
      public static void main(String[] args) throws IOException {
    
    // 1. createDirectory()方法:创建目录
    // 2. createFile()方法:创建文件
    // 3. exists()方法:判断文件或目录是否存在
    // 4. deleteIfExists()方法:如果文件或目录存在,则删除
    
        Path dirPath = Path.of("aaa");
        if (!Files.exists(dirPath)) {
          Files.createDirectory(dirPath);
        }
        Path filePath = Path.of("aaa/bbb");
        System.out.println(filePath.toAbsolutePath());
        Files.deleteIfExists(filePath);
        Files.createFile(filePath);
    
    // 5. writeString()方法:向指定路径的文件写入字符串
    
        Files.writeString(filePath, "第一行\n第二行");
    
    // 6. readString()方法:从指定路径的文件,读取全部内容,返回字符串
    // 7. readAllLines()方法:从指定路径的文件,读取所有行,返回字符串列表
    
        String fileContent = Files.readString(filePath);
        System.out.println(fileContent);
        List<String> allLines = Files.readAllLines(filePath);
        System.out.println(allLines);
    
    // 8. createDirectories()方法:多层目录批量创建,例如 Files.createDirectories(Path.of("aa", "bb", "cc")) 会依次创建 aa bb cc 目录
    // 9. isDirectory()方法:判断指定路径是否是目录
    // 10. size()方法:获取指定路径文件的文件大小,字节单位
    // 11. copy()方法:拷贝源路径文件到目标路径文件
    // 12. move()方法:移动源路径文件到目标路径文件
    // 13. delete()方法:删除文件或目录,不存在或无法删除等异常情况会抛异常
    // 14. walk()方法:指定起始路径,递归遍历,深度优先,包括起始路径
    // 15. 其他方法 ……
    
      }
    }
    /*
    输出
    C:\Users\zhouhuajian\Desktop\demo\aaa\bbb
    第一行
    第二行
    [第一行, 第二行]
     */
    

    Path is shown up only when thousands of people like huajianketang walk through.

    相关文章

      网友评论

          本文标题:Java NIO 的 Files Path 和 Paths

          本文链接:https://www.haomeiwen.com/subject/rnukljtx.html