Skip to content

toaha63/DirectoryTreePrinter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Directory Tree Visualizer 🌳

DirectoryTreePrinter 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.

✨ Features

  • 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

πŸš€ Why This Project?

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.

πŸ“‹ Prerequisites

OpenJDK 21 or later is required. Virtual threads are a feature of Project Loom introduced in Java 21.

Recommended Installation

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.JDK

Verification

Verify your installation:

java -version

Expected output should indicate version 21 or higher.

πŸ› οΈ Installation & Compilation

  1. Clone or Download:

    git clone https://github.com/toaha63/DirectoryTreePrinter.git
    cd DirectoryTreePrinter
  2. Compile the Source:

    javac DirectoryTreePrinter.java

    This generates the '.class' files ready for execution.

πŸ’» Usage

Run the program from the command line with the target directory and optional flags:

Basic Syntax:

java DirectoryTreePrinter <path-to-directory> [flags]

Command Line 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

Examples

Basic usage (current directory):

java DirectoryTreePrinter .

Include hidden files in console output:

java DirectoryTreePrinter /path/to/project -c -h

Export complete structure including hidden files:

java DirectoryTreePrinter /path/to/project -f -h

Real-time view with simultaneous backup:

java DirectoryTreePrinter /path/to/project -b -h

πŸ“– Sample Output

Example 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

βš™οΈ Technical Implementation

Virtual Threads in Action

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

πŸ§ͺ Testing

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 -f

🀝 Contributing

We welcome contributions! Please feel free to submit issues, feature requests, or pull requests.

Potential Enhancements:

  • Depth limiting (--depth flag)
  • File size and metadata display
  • Output format options (JSON, XML)
  • Exclusion patterns (.gitignore support)
  • Colorized output support
  • Graphical user interface (GUI) version

πŸ“Š Performance Notes

  • 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

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™‹β€β™‚οΈ Support

For questions or issues:

  1. Check existing GitHub Issues
  2. Create a new issue with detailed description
  3. Include your Java version and OS environment

Built with β˜• Java 21+ and Virtual Threads for modern development.

About

A Java program to print all directories of a path

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages