Java7 Parh,Paths与Files

Parh,Paths与Files是在JDK7中新添加进来的,都在 java.nio.file包下面,使用时注意JDK版本

一、Parh,Paths

1、java.nio.file.Paths

     作用:通过一个路径字符串来构造 Path对象。

     Paths类是 final修饰的,仅由两个static方法组成。

    • static Path get(String first, String... more)

      将路径字符串或连接到路径字符串的字符串序列转换为 Path

      static Path get(URI uri)

      将给定的URI转换为Path对象。

注意:文件系统的分隔符(Unix文件系统是 / ,Windows是 \,/也可以 )

           路径中的第一个部件可以是根部件,例如 / 或 C:\ 。以根部件开始的是绝对路径;否则就是相对路径。

  

    @Test
    public void testPaths() throws URISyntaxException {
        Path path = Paths.get("E:/java");
        System.out.println(path); // E:\java

        Path path2 = Paths.get("E:/java","/赵云.jpg");
        System.out.println(path2);// E:\java\赵云.jpg

        URI uri = new URI("file:///E:/java/赵云.jpg");
        Path path3 = Paths.get(uri);
        System.out.println(path3); //E:\java\赵云.jpg
    }

2、java.nio.file.Path接口

     可用于在文件系统中定位文件的对象。 它通常表示系统相关的文件路径。

     Path用来表示文件路径和文件。可以有多种方法来构造一个Path对象来表示一个文件路径,或者一个文件。Path接口可以取代File类的。Path类包含一些简单方法操作,具体查看API.

1)判断路径以某一分层路径的开始或结束

    • boolean endsWith(Path other)

      测试此路径是否以给定的路径结束。

      boolean endsWith(String other)

      测试此路径是否以 Path结束,通过转换给定的路径字符串,完全按照 endsWith(Path)方法指定的方式构建。

      boolean startsWith(Path other)

      测试此路径是否以给定的路径开始。

      boolean startsWith(String other)

      测试此路径是否以 Path ,通过转换给定的路径字符串,按照 startsWith(Path)方法指定的方式构建。

    @Test
    public void testPaths() throws URISyntaxException {
        Path path = Paths.get("E:/java","/赵云.jpg");
        System.out.println(path);// E:\java\赵云.jpg

        System.out.println(path.endsWith("jpg"));//false  以某一分层路径
        System.out.println(path.endsWith("赵云.jpg"));//true
        System.out.println(path.startsWith(Paths.get("E:/java")));// true
    }

2)File和Path之间的转换,File和URI之间的转换

    @Test
    public void testPaths() throws URISyntaxException {
        Path path = Paths.get("E:/java","/赵云.jpg");
        File file = path.toFile();
        URI uri = path.toUri();

        file.toPath();
        file.toURI();
    }

二、java.nio.file.Files

      Files类是非常好用的 io操作工具类,该类包含了对文件,目录或其他类型文件进行操作的静态方法。例如文件复制,移动,删除,读取文件内容,写入文件内容等,下面介绍一些常用的方法:

1、文件复制

    @Test
    public void testPaths() throws Exception {
        Path path = Paths.get("E:/java","/赵云image.jpg");
        FileInputStream inputStream = new FileInputStream(new File("E:/java/赵云.jpg"));
        Files.copy(inputStream, path);
    }

2、目录,文件,删除

    @Test
    public void testPaths() throws Exception {
        Path path = Paths.get("E:/java000");
        if(!Files.exists(path)){
            Files.createDirectories(path);
        }
        System.out.println(Files.isDirectory(path)); // true

        Path path2 = Paths.get("E:/java000/text000.txt");
        if(!Files.exists(path2)){
            Files.createFile(path2);
        }

        Files.deleteIfExists(path2);
    }

3、文件的移动

    public void testPaths() throws Exception {
        Path path = Paths.get("E:/java","/赵云image.jpg");
        Path destpath = Paths.get("E:/赵云move.jpg");
        // 移动:原文件不存在,移动并重命名为赵云move.jpg
        Files.move(path,destpath);
    }

4、遍历一个文件夹目录获取文件

    @Test
    public void testPaths() throws Exception {
        Path path = Paths.get("E:/java/file03");

        if(Files.exists(path)){
            DirectoryStream<Path> paths = Files.newDirectoryStream(path);
            paths.forEach(p -> {
                if(!Files.isDirectory(p)){
                    System.out.println(p.getFileName());
                }
            });
        }
    }

              4-        5-

5、遍历一个文件夹目录获取文件,(包含嵌套文件夹中文件)

     walkFileTree方法需要FileVisitor类型的参数,FileVisitor是一个接口,public class SimpleFileVisitor<T> implements FileVisitor<T>

遍历文件和目录时会触发定义在FileVisitor中的方法:

上面4个方法都返回 FileVisitResult 对象,定义了4种访问后的行为。

    @Test
    public void testPaths() throws IOException {
        Path path = Paths.get("E:/java/file03");

        if(Files.exists(path)){
            Files.walkFileTree(path
                , new SimpleFileVisitor<Path>() {
                    // 访问文件时触发
                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        if (file.endsWith("abc雪02.jpg")) {
                            System.out.println("已经找到abc雪02.jpg文件!停止查找!");
                            return FileVisitResult.TERMINATE;
                        }
                        System.out.println("正在访问文件" + file);
                        return FileVisitResult.CONTINUE;
                    }

                    // 访问目录时触发
                    @Override
                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                        System.out.println("正在访问目录:" + dir);
                        return FileVisitResult.CONTINUE;
                    }
                });
        }
    }

       

    

    以官方API为准,自己敲一遍熟悉

ends~

发布了248 篇原创文章 · 获赞 59 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_42402854/article/details/100701792
今日推荐