21xrx.com
2024-12-28 11:59:40 Saturday
登录
文章检索 我的文章 写文章
Java文件目录树及其实现方式
2023-06-15 10:04:02 深夜i     --     --
Java 目录树 文件 代码示例

在Java编程中,目录树结构是经常用到的一种方式,它可以帮助我们组织文件和代码,并方便我们对其进行管理和维护。本文将介绍Java如何实现一个简单的文件目录树结构,并提供代码示例以供参考。

代码实现:

1. 创建一个Directory类,用于表示目录节点:


public class Directory {

  private String name;

  private List subDirectories;

  private List files;

  public Directory(String name) {

    this.name = name;

    subDirectories = new ArrayList<>();

    files = new ArrayList<>();

  }

  // 省略setter和getter方法

  public void addSubDirectory(Directory directory) {

    subDirectories.add(directory);

  }

  public void removeSubDirectory(Directory directory) {

    subDirectories.remove(directory);

  }

  public void addFile(File file) {

    files.add(file);

  }

  public void removeFile(File file) {

    files.remove(file);

  }

}

2. 创建一个File类,用于表示文件节点:


public class File {

  private String name;

  private String content;

  public File(String name, String content)

    this.name = name;

    this.content = content;

  

  // 省略setter和getter方法

  @Override

  public String toString()

    return name;

  

}

3. 创建一个Tree类,用于表示整个目录树:


public class Tree {

  private Directory root;

  public Tree(String rootName) {

    root = new Directory(rootName);

  }

  public Directory getRoot()

    return root;

  

  // 遍历目录树

  public void traverse() {

    System.out.println(root.getName());

    for (Directory subDirectory : root.getSubDirectories()) {

      System.out.println("├──" + subDirectory.getName());

      for (File file : subDirectory.getFiles()) {

        System.out.println("│  ├──" + file.toString());

      }

      traverseHelper(subDirectory.getSubDirectories(), "│  ");

    }

  }

  private void traverseHelper(List subDirectories, String prefix) {

    int size = subDirectories.size();

    for (int i = 0; i < size; i++) {

      Directory subDirectory = subDirectories.get(i);

      if (i == size - 1) {

        System.out.println(prefix + "└──" + subDirectory.getName());

        traverseFiles(subDirectory.getFiles(), prefix + "  ");

        traverseHelper(subDirectory.getSubDirectories(), prefix + "  ");

      } else {

        System.out.println(prefix + "├──" + subDirectory.getName());

        traverseFiles(subDirectory.getFiles(), prefix + "│  ");

        traverseHelper(subDirectory.getSubDirectories(), prefix + "│  ");

      }

    }

  }

  private void traverseFiles(List files, String prefix) {

    for (File file : files) {

      System.out.println(prefix + "├──" + file.toString());

    }

  }

}

4. 编写测试代码,创建一个目录树并遍历:


public static void main(String[] args) {

  Tree tree = new Tree("root");

  Directory dir1 = new Directory("dir1");

  Directory dir2 = new Directory("dir2");

  Directory dir3 = new Directory("dir3");

  File file1 = new File("file1.txt", "hello world");

  File file2 = new File("file2.txt", "hello java");

  File file3 = new File("file3.txt", "hello world");

  tree.getRoot().addSubDirectory(dir1);

  tree.getRoot().addSubDirectory(dir2);

  tree.getRoot().addSubDirectory(dir3);

  dir1.addFile(file1);

  dir2.addFile(file2);

  dir3.addFile(file3);

  tree.traverse();

}

输出结果为:


root

├──dir1

│  ├──file1.txt

├──dir2

│  ├──file2.txt

└──dir3

  ├──file3.txt

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复