zoukankan      html  css  js  c++  java
  • java nio 的使用

    java nio 用法

    package com.example.javademo.file;
    
    import com.example.javademo.JavaDemoApplicationTests;
    import org.junit.Test;
    import org.springframework.util.FileSystemUtils;
    
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.*;
    import java.nio.file.attribute.BasicFileAttributes;
    import java.util.Arrays;
    
    public class PathTest extends JavaDemoApplicationTests {
    
    
        /**
         * 创建目录
         * @throws IOException
         */
        @Test
        public void createDirectory() throws IOException {
            //获取path直接创建 C:/test
            System.out.println(Files.createDirectory(Paths.get("C:/test")));
             //创建多层次的目录   可以在path之后添加路径  C:/test/tt
            System.out.println(Files.createDirectories(Paths.get("C:/test/aa").resolve("tt")));
            /**
             * 创建临时目录: 历史目录是在你的系统中个人用户的临时文件夹下创建
             */
    
            //如果不加系统根目录 ,默认添加到系统用户的临时文件中  C:UserslcmAppDataLocalTempaa4464090953349324592
            System.out.println(Files.createTempDirectory("aa"));
            // 1. 添加了根目录就会在根目录下生成文件  C:	est	em8169131100444631705.txt  2.如果不添加first  那么就会在项目根目录下创建一个文件  tem2136575820052041908.txt
            System.out.println(Files.createTempFile(Paths.get("/test"),"tem",".txt"));
    
        }
    
        /**
         * 删除目录
         * @throws IOException
         */
        @Test
        public void deleteDirectory() throws IOException {
            /*//普通删除一个目录
            Files.delete(Paths.get("C:/test/aa/tt"));*/
            //删除指定目录  如果存在就删除,只能删除空的目录
            File file = new File("C:/test/aa");
            if (file.isFile()){
                file.delete();
            }
    
            Arrays.asList(file.listFiles()).forEach(FileSystemUtils::deleteRecursively);
            //System.out.println(Files.deleteIfExists(Paths.get("C:/test/aa")));
        }
    
    
        /**
         * 获取指定目录下的所有文件 Files.list()   Files.lines()
         * @throws IOException
         */
        @Test
        public void list() throws IOException {
    
            //获取该目录下的所有的文件
            Files.list(Paths.get("C:/test")).forEach(s->{
                System.out.println(s.getFileName());
                System.out.println(s);
            });
        }
    
        /**
         * 判断系统文件是否存在
         * @throws IOException
         */
        @Test
        public void exists() throws IOException {
            //其中第二个参数是如何确定该文件是否存在,LinkOption.NOFOLLOW_LINKS 说明不应该在文件系统中跟踪符号链接来判断是否文件存在
            System.out.println(Files.exists(Paths.get("C:/isExists"),new LinkOption[]{LinkOption.NOFOLLOW_LINKS} ));
        }
    
        /**
         * 复制文件
         * @throws IOException
         */
        @Test
        public void copy() throws IOException {
            //第一个参数是源文件  ,第二个参数目的文件,第三个参数是移动的方式取值有三个, COPY_ATTRIBUTES讲属性复制到新的文件,REPLACE_EXISTING如果目标文件存在就覆盖,ATOMIC_MOVE讲源文件一原子的操作进行移动到目标位置
            System.out.println(Files.copy(Paths.get("C:/test/aa/tt/新建 RTF 文档.rtf"),Paths.get("C:/test/aa/ff/新建 RTF 文档.rtf"), StandardCopyOption.COPY_ATTRIBUTES));
        }
    
        /**
         * 移动文件到指定的目录下
         * @throws IOException
         */
        @Test
        public void move() throws IOException {
            System.out.println(Files.move(Paths.get("C:/test/aa/tt/新建 DOC 文档.doc"),Paths.get("C:/test/aa/ff/新建 DOC 文档.doc"),StandardCopyOption.ATOMIC_MOVE));
        }
    
        /**
         *遍历制定路径下的所有文件夹和文件以及子文件夹和子文件
         * 四个方法:
         * preVisitDirectory  进入文件夹之前
         * visitFile   正在访问文件时、
         * visitFileFailed  文件访问失败时
         * postVisitDirectory  从文件下出来后
         * @throws IOException
         */
        @Test
        public void walkFileTree() throws IOException {
            Files.walkFileTree(Paths.get("C:/test"), new FileVisitor<Path>() {
                //
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    if (Files.list(dir).count()<0){
                        return FileVisitResult.SKIP_SUBTREE;
                    }
                    System.out.println(dir);
                    return FileVisitResult.CONTINUE;
                }
    
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    System.out.println(file);
                    return FileVisitResult.CONTINUE;
                }
    
                @Override
                public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                    System.out.println(file);
                    return FileVisitResult.CONTINUE;
                }
    
                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    System.out.println(dir);
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    
    }
    
    
    
  • 相关阅读:
    《Programming WPF》翻译 第8章 1.动画基础
    一些被遗忘的设计模式
    《Programming WPF》翻译 第4章 数据绑定
    《Programming WPF》翻译 第3章 控件
    《Programming WPF》翻译 第5章 样式和控件模板
    《Programming WPF》翻译 第7章 绘图
    《Programming WPF》翻译 第9章 自定义控件
    《Programming WPF》翻译 第7章 绘图 (2)
    《Programming WPF》翻译 第8章 前言
    关于Debug和Release之本质区别
  • 原文地址:https://www.cnblogs.com/java-hardly-road/p/11114878.html
Copyright © 2011-2022 走看看