netty中文件的遍历、删除、复制和传输

netty中文件的使用

1. 遍历文件夹和文件

  /**
     * 遍历文件夹和文件
     *
     * @throws IOException
     */
    private static void m1() throws IOException {
        // 计数器
        AtomicInteger dirCount = new AtomicInteger();
        AtomicInteger fileCount = new AtomicInteger();
        Files.walkFileTree(Paths.get("E:\\ideaWork\\netty_study\\target"), new SimpleFileVisitor<Path>() {
            /**
             * 遍历文件夹
             * 此方法在遍历目录之前使用
             * @param dir
             * @param attrs
             * @return
             * @throws IOException
             */
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("目录名称为==========》" + dir);
                dirCount.incrementAndGet();
                return super.preVisitDirectory(dir, attrs);
            }

            /**
             * 遍历文件
             * 此方法在遍历文件之前使用
             * @param file
             * @param attrs
             * @return
             * @throws IOException
             */
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println("文件名称为==========》" + file);
                fileCount.incrementAndGet();
                return super.visitFile(file, attrs);
            }
        });
        System.out.println("dir count:" + dirCount);
        System.out.println("file count:" + fileCount);
    }

2. 遍历某一类文件有多少个

    /**
     * 遍历某一类文件有多少个
     * @throws IOException
     */
    private static void m2() throws IOException {
        AtomicInteger classCount = new AtomicInteger();
        Files.walkFileTree(Paths.get("E:\\ideaWork\\netty_study\\target"), new SimpleFileVisitor<Path>() {
            /**
             * 遍历文件
             * 此方法在遍历文件之前使用
             * @param file
             * @param attrs
             * @return
             * @throws IOException
             */
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.toString().endsWith(".class")) {
                    System.out.println(file);
                    classCount.incrementAndGet();
                }
                return super.visitFile(file, attrs);
            }
        });
    }

3. 删除多级目录

 /**
     * 删除多级目录  直接删除了  没有备份,回收站里面找不到
     * @throws IOException
     */
    private static void m3() throws IOException {
        Files.walkFileTree(Paths.get("E:\\ideaWork\\netty_study\\target"), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return super.postVisitDirectory(dir, exc);
            }
        });
    }

4.文件复制

	/**
     * 文件复制
     * @throws IOException
     */
	 public static void m4() throws IOException {
	        // 源目录
	        String source = "E:\\ideaWork\\netty_study\\target\\classes";
	        // 目标目录
	        String target = "C:\\Users\\Administrator\\Desktop\\zzh";
	        Files.walk(Paths.get(source)).forEach(path -> {
	            try {
	                String targetName = path.toString().replace(source, target);
	                // 是目录
	                if (Files.isDirectory(path)) {
	                    // 如果目录存在,报错
	                    Files.createDirectory(Paths.get(targetName));
	                }
	                // 是文件
	                if (Files.isRegularFile(path)) {
	                    Files.copy(path, Paths.get(targetName));
	                }
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        });
	    }

5.文件传输

 /**
     * 两个文件之间的数据传输
     * 效率高,底层会利用操作系统的零拷贝进行优化
     */
    public static void m() {
        try (
                // 获取文件数据
                FileChannel from = new FileInputStream("data.txt").getChannel();
                // 写入文件数据
                FileChannel to = new FileOutputStream("to.txt").getChannel()
        ) {
            // 效率高,底层会利用操作系统的零拷贝进行优化,只能传输2g数据
            //  from.transferTo(0,from.size(),to);
            long size = from.size();
            // left 表示还剩下多少字节
            for (long left = size; left > 0; ) {
                left -= from.transferTo((size - left), left, to);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值