首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么列表保持不变?

为什么列表保持不变?
EN

Stack Overflow用户
提问于 2017-03-07 21:19:07
回答 1查看 58关注 0票数 0

我尝试创建一个小应用程序,它使用文件遍历器递归地读取文件系统中指定位置的内容。我有这样的代码:

代码语言:javascript
复制
/* Imports here */

public class App {

    private static Scanner inp;
    public static List<Node> Tree = new ArrayList<Node>();

    public static void main(String[] args) {
        String FFiles;

        System.out.print("Enter root directory: ");
        inp = new Scanner(System.in);

        FileSystem fileSystem = FileSystems.getDefault();
        Path rootPath = fileSystem.getPath(inp.nextLine());

        FileVisitor<Path> simpleFileVisitor = new SimpleFileVisitor<Path>() {
              public Path VisitedDirectory = rootPath;
              public List<Node> files = new ArrayList<Node>();


              @Override
              public FileVisitResult preVisitDirectory(Path dir,BasicFileAttributes attrs) throws IOException {
                if (dir != VisitedDirectory) {
                    Node directory = new Node(dir.getFileName().toString(), "directory", files, dir.toString());
                    files.add(directory);
                    Node VisDirectory = new Node(VisitedDirectory.getFileName().toString(), "directory", files, dir.toString());
                    Tree.add(VisDirectory);

                    files.clear();
                }
                return FileVisitResult.CONTINUE;
              }

              @Override
              public FileVisitResult visitFile(Path visitedFile,BasicFileAttributes fileAttributes) throws IOException {
                Node file = new Node(visitedFile.getFileName().toString(), "file", null, visitedFile.toString());
                files.add(file);
                return FileVisitResult.CONTINUE;
              }
        };



        try {
            Files.walkFileTree(rootPath, simpleFileVisitor);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        for (Node n : Tree) {
            System.out.println(n);
        }
    }

这里有一个问题:为什么它只读取(显示)目录?P.S Node类只是几个变量,声明如下:

代码语言:javascript
复制
public class Node {
    @Override
    public String toString() {
        return "Node [path=" + path + "]";
    }

    public String name;
    public String path;
    public  String type;
    public List<Node> children = new ArrayList<Node>();
    public Node(String name, String type, List<Node> children, String path) {
        this.name = name;
        this.type = type;
        this.children = children;
        this.path = path;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-07 21:25:21

你把files列表弄得乱七八糟。您只能创建单个实例。因此,您的new Node(VisitedDirectory.getFileName().toString(), "directory", files, dir.toString())实际上并不复制文件列表,而只是存储一个引用。您的files.clear()调用将清除您拥有的所有文件列表。这就是为什么只有您添加到Tree中的节点才能存活。

请将Tree重命名为小写。这样,人们就会认为Tree.add()调用了Tree类上的静态方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42649400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档