- <span style="font-size:18px;"><strong>package com.j2se;
- import java.io.File;
- public class FileList {
- /**
- * @param args
- */
- public static void main(String[] args) {
- File f = new File("d:/A");
- tree(f); // 调用该方法。
- }
- private static void tree(File f){
- File[] childs = f.listFiles();
- for(int i=0;i<childs.length;i++){
- System.out.println(childs[i].getName());
- if(childs[i].isDirectory()){
- tree(childs[i]);
- }
- }
- }
- }</strong></span>
- <span style="font-size:18px;"><strong>以上 这个方法:没有层次感。</strong></span>
- <span style="font-size:18px;"><strong></strong></span><pre name="code" class="java">import java.io.*;
- public class FileList {
- public static void main(String[] args) {
- File f = new File("d:/A");
- System.out.println(f.getName()); // 打印A
- tree(f, 1);
- }
- private static void tree(File f, int level) {
- String preStr = "";
- for(int i=0; i<level; i++) {
- preStr += " ";
- }
- File[] childs = f.listFiles();
- for(int i=0; i<childs.length; i++) {
- System.out.println(preStr + childs[i].getName());
- if(childs[i].isDirectory()) {
- tree(childs[i], level + 1);
- }
- }
- }
- }