Given a path to a directory, print the path to the biggest file in it Here is my code. I simulated a stack rather than used some regular recursive algorithm because I needed to keep track of two variables: the path to the biggest file and its size. Besides I wrote a few tests.

My solution on Github.

DirectoryIterator.java

import java.io.File;
import java.util.ArrayDeque;
import java.util.Deque;

public class DirectoryIterator {

    private DirectoryIterator() {
        throw new AssertionError();
    }

    public static String getLargestFilePath(File initPath) {
        if (initPath == null) {
            throw new IllegalArgumentException("The init path cannot be null");
        }
        long maxFileLength = 0;
        String maxFilePath = null;
        Deque&lt;File&gt; stack = new ArrayDeque&lt;&gt;();
        stack.push(initPath);
        while (!stack.isEmpty()) {
            File current = stack.pop();
            if (current.isDirectory()) {
                File[] files = current.listFiles();
                if (files != null) {
                    for (File file : files) {
                        stack.push(file);
                    }
                }
            } else if (current.length() &gt; maxFileLength) {
                maxFileLength = current.length();
                maxFilePath = current.getAbsolutePath();
            }
        }
        return maxFilePath;
    }

}


DirectoryIteratorTest.java

import org.junit.Assert;
import org.junit.Test;

import java.io.File;

public class DirectoryIteratorTest {

    private String mTestDir = "C:\\Users\\Maksim\\Downloads\\Test" + File.separatorChar;

    @Test
    public void singleFileIsInit() {
        File initPath = new File(mTestDir + "Android_Accelerometer.png");
        Assert.assertEquals(initPath.getAbsolutePath(), DirectoryIterator.getLargestFilePath(initPath));
    }

    @Test
    public void onlyDirs() {
        File initPath = new File(mTestDir + "only_dirs");
        Assert.assertEquals(null, DirectoryIterator.getLargestFilePath(initPath));
    }

    @Test(expected = IllegalArgumentException.class)
    public void nullPath() {
        Assert.assertEquals(null, DirectoryIterator.getLargestFilePath(null));
    }

    @Test
    public void emptyPath() {
        File initPath = new File("");
        Assert.assertEquals(null, DirectoryIterator.getLargestFilePath(initPath));
    }

    @Test
    public void manyFiles() {
        File initPath = new File("C:\\Users\\Maksim\\Google Drive");
        Assert.assertEquals("C:\\Users\\Maksim\\Google Drive\\Career\\TODO\\AnDevCon Boston 2015.zip", DirectoryIterator.getLargestFilePath(initPath));
    }
}
I find it weird that your function accepts a File but returns a String.  I would choose to return a File.

According to the JavaDoc for listFiles(),


  If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of File objects is returned, one for each file or directory in the directory.


Based on the documented behaviour, I would say that your if (current.isDirectory()) and if (files != null) checks are redundant, and I would eliminate the current.isDirectory() check.

It might be a good idea to do something about the possible SecurityException if you don't have permission to list a directory.

The throw new AssertionError() in the private constructor is an interesting idea that I haven't seen before.  Perhaps it would be better written as assert false;.