Discuss / Java / 1

波猫747

#1 Created at ... [Delete] [Delete and Lock User]

import java.io.File;

public class FileExercise { static int deepth = 0;

public static void main(String[] args) {
    File file = new File("Documents");
    listAllFile(file);
}

static void listAllFile(File file) {
    for (int i = 1; i <= deepth; i++) {
        System.out.print("  ");
    }
    String[] split = file.getPath().split("\\\\");
    System.out.println(split[split.length-1]+"\\");
    File[] files = file.listFiles();
    for (File f : files) {
        if (f.isDirectory()) {
            deepth++;
            listAllFile(f);
            deepth--;
        } else {
            deepth++;
            for (int i = 1; i <= deepth; i++) {
                System.out.print("  ");
            }
            String[] fileSpilt = f.getPath().split("\\\\");
            System.out.println(fileSpilt[fileSpilt.length-1]);
            deepth--;

        }
    }

}

}

波猫747

#2 Created at ... [Delete] [Delete and Lock User]
import java.io.File;

public class ReFileExercise {
    public static void main(String[] args) {
        File file = new File("Documents");
        FileTool.listAllFile(file);
    }
}

class FileTool {
    private static int deepth = 0;

    public static void listAllFile(File file) {

        String[] diectSplit = file.getPath().split("\\\\");
        printBlank(deepth);
        System.out.println(diectSplit[diectSplit.length - 1]+"\\");


        File[] files = file.listFiles();
        for (File f : files) {
            deepth++;
            if (f.isFile()) {
                String[] fileSplit = f.getPath().split("\\\\");
                printBlank(deepth);
                System.out.println(fileSplit[fileSplit.length - 1]);
            } else if (f.isDirectory()) {
                listAllFile(f);
            }
            deepth--;
        }
    }

    private static void printBlank(int num) {
        for (int i = 0; i < num; i++) {
            System.out.print("  ");
        }
    }
}

  • 1

Reply