-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

0

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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);
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.