Walking a File Tree in Java – Part 3

Stylised image of a man walking along a road

In the last two posts, we’ve looked at ways of walking a file tree. In part 1 we used the Visitor design pattern. In part 2 we revised the concepts of stream processing and wrote a very simple program using this stream-based approach.

This week we’ll use the Stream API again, this time doing some more elaborate stream processing.

AI Code Generation

Over the last few months, there’s been a huge amount of interest and discussion around the idea of using AI (Artificial Intelligence) as a way of generating code (as well as everything else!). So as an experiment, I thought I’d put an AI LLM (Large Language Model) to the test.

I used the Bing chat-bot/“copilot” in Microsoft’s Edge browser. Not because it’s my browser of choice, but because it’s easily accessible to most people, and it doesn’t need registration for limited use.

My initial prompt was “Generate a Java program using the Stream API to walk a file tree and sort the results by the number of files in the directory and the total size of the files in the directory”. I had to refine my prompt once or twice to get the results I wanted. Mainly I was just more specific about the sorted data and the sort order.

After I tweaked the output and changed the formatting, the results were exactly what I wanted! I could have refined my prompts even further to get Bing to generate the code, but it was easier for me to make the changes myself.

Here’s the code it generated, followed by the generated explanations.

The Generated Code

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.Map;
import java.util.stream.Collectors;

public class FileTreeWalker {

    public static void main(String[] args) throws IOException {

        // Change this to the starting directory of the file tree
        Path start = Paths.get("C:\\"); 

        Map<Path, Long> fileCount = Files.walk(start)
                .filter(Files::isRegularFile)
                .collect(Collectors.groupingBy(Path::getParent, 
                                               Collectors.counting()));

        Map<Path, Long> dirSize = Files.walk(start)
                .filter(Files::isRegularFile)
                .collect(Collectors.groupingBy(Path::getParent, 
                     Collectors.summingLong(p -> p.toFile().length())));

        Files.walk(start)
                .filter(Files::isDirectory)
                .sorted(Comparator.comparing(
                          p -> dirSize.getOrDefault(p, 0L)).reversed())
                .forEach(p -> System.out.printf(
                         "size: %,16d; files: %4d; dir: %s%n", 
                         dirSize.getOrDefault(p, 0L), 
                         fileCount.getOrDefault(p, 0L), p));

    }
} // end of class

The Generated Explanations

“Here is a Java program that uses the Stream API to walk a file tree and sort the results by the number of files in the directory and the total size of the files in the directory.”

“This program starts at a specified directory (C:\ in this example) and walks the file tree. It uses the Files.walk method to create a stream of Path objects representing all the files and directories in the tree. It then filters out only regular files using the Files.isRegularFile method. The stream is then collected into a Map where the keys are the parent directories of the files and the values are the number of files in each directory. This is done using the Collectors.groupingBy and Collectors.counting collectors.”

“The program then creates another stream of Path objects representing all the files in the tree and collects it into a Map where the keys are the parent directories of the files and the values are the total size of all files in each directory. This is done using the Collectors.groupingBy and Collectors.summingLong collectors.”

“Finally, the program creates another stream of Path objects representing all directories in the tree and sorts it using a custom comparator that compares directories based on their file count and total size. The sorted stream is then printed to standard output.”

“You can modify this program to suit your needs by changing the starting directory or by adding additional filtering or sorting criteria.”

Conclusions

I was very impressed! It was a quick and painless experience. As a productivity aid, I would give it an 8 or 9 out of 10. As a Java learning and growing experience, I’d only rate it a 1 or 2 out of 10. I’m used to reading manuals and pouring over documentation, often coding by trial and error. “Blood, sweat and tears” is the experience that jumps to mind. But also feelings of elation, accomplishment and satisfaction when my code works the way I want it to. I didn’t experience any of that. It felt like cheating.

I found that I felt grateful, and politely thanked Bing after each prompt. Bing generally responded with “You’re welcome! Let me know if you have any questions or need further assistance.” With a smiley…

Have you used AI to generate code? Did it make it easier to write your application? Please share your comments on the blog post.

Stay safe and keep learning!

An Aside

My experience with generating code with AI was a positive one! I hope yours will be as good.

However, when I asked Bing to generate a constitution for a new sporting club we are starting, its output was a lot less useful. The response was “It’s great to hear that you are starting a new association! When creating a constitution for your association, it can be helpful to look at the constitutions of similar respected and established associations for guidance.” This was essentially a restatement of my prompt. Bing then summarised the missions and structures of the two example organisations I had named in my prompt.

So AI isn’t the silver bullet or ideal solution for every problem. Horses for courses…

Leave a Comment

Your email address will not be published. Required fields are marked *

Code like a Java Guru!

Thank You

We're Excited!

Thank you for completing the form. We're excited that you have chosen to contact us about training. We will process the information as soon as we can, and we will do our best to contact you within 1 working day. (Please note that our offices are closed over weekends and public holidays.)

Don't Worry

Our privacy policy ensures your data is safe: Incus Data does not sell or otherwise distribute email addresses. We will not divulge your personal information to anyone unless specifically authorised by you.

If you need any further information, please contact us on tel: (27) 12-666-2020 or email info@incusdata.com

How can we help you?

Let us contact you about your training requirements. Just fill in a few details, and we’ll get right back to you.

Your Java tip is on its way!

Check that incusdata.com is an approved sender, so that your Java tips don’t land up in the spam folder.

Our privacy policy means your data is safe. You can unsubscribe from these tips at any time.