HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Given a path to a directory, print the path to the biggest file in it

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
paththedirectoryfilebiggestprintgiven

Problem

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 stack = new ArrayDeque<>();
        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() > 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(expec

Solution

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

Context

StackExchange Code Review Q#117823, answer score: 5

Revisions (0)

No revisions yet.