Home >Java >javaTutorial >How to traverse a folder in java
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); } } } }
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!