 <p>You need to add recursion depth to your recursive method, to be able to limit what is printed:</p> <pre><code>long getDirSize(File dir, int depth, int printDepth) </code></pre> <p>Then you need recursion call like this:</p> <pre><code>size += this.getDirSize(file, depth+1, printDepth); </code></pre> <p>And if you meant you want to print only sizes at maxdepth, then you need to add test like</p> <pre><code>if (depth == printDepth) { // or depth &lt;= printDepth maybe // ...do printing only for these } </code></pre> <hr> <p>It might make sense to wrap the whole thing in a class, so you could make printDepth a member variable, and the recursive method private, something like:</p> <pre><code>class DirSizePrinter { int printDepth; File root; public DirSizePrinter(int printDepth, File root) { this.printDepth = printDepth; this.root = root; } public long printSize() { return printSizeRecursive(0); } private long printSizeRecursive(int depth) { // ... code from question with depth added, and using printDepth and root } } </code></pre> <p>Usage:</p> <pre><code> new DirSizePrinter(3, "C:/temp").printSize(); </code></pre> <p>Or some variation of this, depending on all the requirements you have.</p>