Home > Java > javaTutorial > How to traverse a folder in java

How to traverse a folder in java

PHPz
Release: 2023-05-03 19:22:12
forward
2124 people have browsed it

Traverse folders

  public void printFiles(File dir,int tab) {//tab为不同目录结构的缩进量
    if (dir.isDirectory()) {
      File next[] = dir.listFiles();//判断如果是目录 则返回目录所有的文件名数组用于遍历文件夹
      for (int i = 0;i<next.length;i++) {//层次缩进输出
        System.out.print("---");
      }
      for(int i = 0;i<next.length;i++) {//这里用了递归获取目录结构
        System.out.println(next[i].getName());
        if (next[i].isFile()) {
          printFiles(next[i],++tab);

        }
      }
    }
  }
Copy after login

The above is the detailed content of How to traverse a folder in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template