-1

I've (as a Java beginner) got the following problem. I want to Display the Tree hierarchy from a directory (same or kind of the same you can do with windows CMD with: tree C:/)

hope for quick response

2 Answers2

0

You can get the whole File System hierarchy using File.listFiles()

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • 1
    This is true, but slightly misleading: you can get the files in a directory using `File.listFiles()`, and you can call `listFiles` on those files recursively; but you don't automatically get the full hierarchy simply by calling that method. – Andy Turner Sep 06 '16 at 15:52
0

The listFiles() method in java.io.File lists the files in a directory. Starting from there, you can go through the directory tree recursively:

public static void main(String[] args)
{
    listDirectory(new File("C:/"), 0);
}

private static void listDirectory(File directory, int level)
{
    for(File file : directory.listFiles())
    {
        for(int i = 0; i < level; i++)
            System.out.print('\t');
        System.out.println(file.getName());
        if(file.isDirectory())
            listDirectory(file, level + 1);
    }
}
Beethoven
  • 375
  • 1
  • 8