A modern, high-performance Java command-line application that generates clean, hierarchical directory structure visualizations. Built with Java 21+, this tool leverages virtual threads for efficient non-blocking I/O operations, demonstrating modern Java concurrency patterns in a practical utility.
- Visual Tree Output: Creates professional directory trees using Unicode box-drawing characters
- Multiple Output Modes: Console output, file export, or simultaneous both
- Hidden File Support: Optional inclusion of hidden files and directories (
.git,.config, etc.) - Modern Concurrency: Utilizes Java 21's virtual threads for non-blocking file I/O operations
- Sorted Listing: Alphabetical sorting with directories grouped before files
- Cross-Platform: Works seamlessly on Windows, Linux, and macOS
- Zero Dependencies: Single Java file implementation - just compile and run
This tool serves as an excellent example of applying cutting-edge Java concurrency features to solve real-world problems. It demonstrates how virtual threads can dramatically simplify asynchronous programming while maintaining performance and readability, making it an ideal learning resource for modern Java development.
OpenJDK 21 or later is required. Virtual threads are a feature of Project Loom introduced in Java 21.
Eclipse Temurin (OpenJDK) 21+ is recommended for best performance and licensing:
# Ubuntu/Debian
sudo apt install openjdk-21-jdk
# macOS (Homebrew)
brew install openjdk@21
# Windows (Winget)
winget install EclipseAdoptium.Temurin.21.JDKVerify your installation:
java -versionExpected output should indicate version 21 or higher.
-
Clone or Download:
git clone https://github.com/toaha63/DirectoryTreePrinter.git cd DirectoryTreePrinter -
Compile the Source:
javac DirectoryTreePrinter.java
This generates the '.class' files ready for execution.
Run the program from the command line with the target directory and optional flags:
Basic Syntax:
java DirectoryTreePrinter <path-to-directory> [flags]| Flag | Description | Example |
|---|---|---|
| (none) | Default: Print to console, exclude hidden files | java DirectoryTreePrinter . |
-c |
Console output only | java DirectoryTreePrinter . -c |
-f |
File output only (generates <dirname>_tree.txt) |
java DirectoryTreePrinter . -f |
-b |
Both console and file output | java DirectoryTreePrinter . -b |
-h |
Include hidden files and directories | java DirectoryTreePrinter . -h |
Basic usage (current directory):
java DirectoryTreePrinter .Include hidden files in console output:
java DirectoryTreePrinter /path/to/project -c -hExport complete structure including hidden files:
java DirectoryTreePrinter /path/to/project -f -hReal-time view with simultaneous backup:
java DirectoryTreePrinter /path/to/project -b -hExample output for a development project:
my-project/
βββ .git/
β βββ HEAD
β βββ config
β βββ refs/
βββ .gitignore
βββ src/
β βββ main/
β β βββ java/
β β β βββ com/
β β β βββ example/
β β β βββ App.java
β β βββ resources/
β β βββ application.properties
β βββ test/
β βββ java/
β βββ com/
β βββ example/
β βββ AppTest.java
βββ target/
β βββ classes/
β β βββ com/
β β βββ example/
β β βββ App.class
β β βββ AppTest.class
β βββ my-app.jar
βββ pom.xml
βββ README.md
This application demonstrates practical use of Project Loom's virtual threads for I/O-bound operations:
Thread virtualThread = Thread.startVirtualThread(() ->
{
try (PrintWriter writer = new PrintWriter(new FileWriter(outputFileName)))
{
// Non-blocking file write operation
printDirectoryTreeToFile(rootDir, new Vector<>(), writer, showHidden);
System.out.println("Directory tree successfully written to: " + outputFileName);
}
catch (IOException e)
{
exceptionRef.set(e);
}
});Performance Benefits:
- Non-blocking I/O: File operations don't block carrier threads
- Resource efficiency: Millions of virtual threads can coexist with minimal overhead
- Simplified code: Maintains synchronous style without callback complexity
Hidden File Detection
Uses Java's built-in isHidden() method for cross-platform compatibility:
if (showHidden || !file.isHidden())
{
filteredFiles.add(file);
}Test the application with various directory structures:
# Test with home directory (excluding hidden files)
java DirectoryTreePrinter ~
# Test with system directories (including hidden files)
java DirectoryTreePrinter /etc -h
# Test output to file
java DirectoryTreePrinter /var/log -fWe welcome contributions! Please feel free to submit issues, feature requests, or pull requests.
Potential Enhancements:
- Depth limiting (
--depthflag) - File size and metadata display
- Output format options (JSON, XML)
- Exclusion patterns (
.gitignoresupport) - Colorized output support
- Graphical user interface (GUI) version
- Memory Efficient: Uses vectors for thread-safe operations with minimal overhead
- Scalable: Handles directories with thousands of files efficiently
- Fast Execution: Virtual threads ensure responsive performance during I/O operations
This project is licensed under the MIT License - see the LICENSE file for details.
For questions or issues:
- Check existing GitHub Issues
- Create a new issue with detailed description
- Include your Java version and OS environment
Built with β Java 21+ and Virtual Threads for modern development.