Paths和Files类的使用

Paths

用于来表示文件路径和文件

@Test
    public void testPath() throws IOException {
        //构造Path类对象
        File file = new File("F:/Workspaces/blog2");
        System.out.println(file.toURI()); // F:/Workspaces/blog2
        Path path = file.toPath();

        URI uri = URI.create("file:///F:/Workspaces/blog3");
        Path path1 = Paths.get(uri);

        Path path2 = Paths.get("E:/", "blog1");
        Path path3 = Paths.get("E:/blog2");

        Path path4 = FileSystems.getDefault().getPath("E:/", "blog4");

        //判断是否存在
        System.out.println(Files.exists(path));

        //如果文件(目录)不存在创建文件
        if (!Files.exists(path1)) {
            Files.createDirectory(path1);//创建目录
        }
        if (!Files.exists(path2)) {
            Files.createFile(path2);//创建文件
        }
    }

Files 类

  1. 文件复制
public static void main(String[] args) throws IOException { 
  Files.copy(Paths.get("E:/1234/db_wego.sql"),System.out);
  Files.copy(System.in,Paths.get("E:/aa.txt"), StandardCopyOption.REPLACE_EXISTING);
  Files.copy(Paths.get("E:/1234/db_wego.sql"),Paths.get("E:/aa.txt"),StandardCopyOption.REPLACE_EXISTING);
}

相关方法介绍:

  • 从文件复制到文件:Files.copy(Path source, Path target, CopyOption options);
  • 从输入流复制到文件:Files.copy(InputStream in, Path target, CopyOption options);
  • 从文件复制到输出流:Files.copy(Path source, OutputStream out);
  1. 写文件
@Test
public void fun4() throws IOException {
   BufferedWriter bw = Files.newBufferedWriter(Paths.get("E:/1234/dd.sql"), StandardCharsets.UTF_8);
   bw.write("haha");
   bw.flush();
   bw.close();
}
  1. 读文件
@Test
public void fun5() throws IOException {//
   BufferedReader br = Files.newBufferedReader(Paths.get("E:/1234/db_wego.sql"), StandardCharsets.UTF_8);
   //注意:如果指定的字符编码不对,可能会抛出MalformedInputException异常
   String res = null;
   while ((res = br.readLine()) != null) {
       System.out.println(res);
   }
   br.close();
}

  1. 遍历文件夹
@Test
public void fun6() throws IOException {
  Path path = Paths.get("E:/1234");
  //方式一
  Files.newDirectoryStream(path)
          .forEach(item -> System.out.println(item.getFileName()));
  //方式二
  Files.list(path).forEach(item -> System.out.println(item.getFileName()));
}

  1. 递归遍历文件夹
@Test
public void fun7() throws IOException {
   Path root = Paths.get("E:/Workspaces/Project/Blog/src/test/java/com/hc");
   List<Path> result = new LinkedList<>();
   Files.walkFileTree(root, new FindJavaVisitor(result));
   result.forEach(System.out::println);
}

private   class FindJavaVisitor extends SimpleFileVisitor<Path> {
   private List<Path> res;
   public FindJavaVisitor(List<Path> result){
       this.res = result;
   }
   @Override
   public FileVisitResult visitFile(Path file, BasicFileAttributes attrs){
       if(file.toString().endsWith(".java")){
           res.add(file.getFileName());
       }
       return FileVisitResult.CONTINUE;
   }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

缘不易

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值