cmylyk923 2017-03-18 01:55 采纳率: 0%
浏览 1926
已结题

java用walkFileTree访问目录树

package com.cmy.niobiocio;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class B {
public static void main(String []args){
findCDirectoryTree();
}
public static void findCDirectoryTree(){
Path p1=FileSystems.getDefault().getPath("D://");
try {
Files.walkFileTree(p1,new SimpleFileVisitor(){

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                // TODO Auto-generated method stub
                if(file.toString().endsWith(".txt")){
                    System.out.println(file.getFileName());
                }
                return FileVisitResult.CONTINUE;
            }

        });
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}
运行结果如下:
java.nio.file.AccessDeniedException: D:\$RECYCLE.BIN\S-1-5-21-4206971208-3045675379-530765779-1002
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsDirectoryStream.(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newDirectoryStream(Unknown Source)
at java.nio.file.Files.newDirectoryStream(Unknown Source)
at java.nio.file.FileTreeWalker.walk(Unknown Source)
at java.nio.file.FileTreeWalker.walk(Unknown Source)
at java.nio.file.FileTreeWalker.walk(Unknown Source)
at java.nio.file.FileTreeWalker.walk(Unknown Source)
at java.nio.file.Files.walkFileTree(Unknown Source)
at java.nio.file.Files.walkFileTree(Unknown Source)
at com.cmy.niobiocio.B.findCDirectoryTree(B.java:20)
at com.cmy.niobiocio.B.main(B.java:15)
出现了以上错误,求各位大神指点一下

  • 写回答

2条回答 默认 最新

  • oyljerry 2017-03-19 02:30
    关注

    Access deny

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀