Scanning a directory and listing contents in an HTML file My program simply accepts the name of a directory from the user, scans said directory and generates an HTML file that lists the contents.

Is there any way that this can be improved?

/*
    Basically, take the path of a directory, get a list of all files in the directory
    and output that list to an html document
*/
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedWriter;
import java.nio.file.FileSystemException;

public class HTMLFileListGenerator{
    static void writeDirectoryFilesToHTML(File directory) throws IOException{
        ArrayList&lt;String&gt; contents = new ArrayList&lt;String&gt;(Arrays.asList(directory.list()));

        File output = new File("output.html");
        try(BufferedWriter writer = new BufferedWriter(new FileWriter(output))){
            writer.write("&lt;html&gt;\n\t&lt;body&gt;\n\t\t&lt;h1&gt;Contents of " + directory.getCanonicalPath() + ":&lt;/h1&gt;\n\t\t&lt;ul&gt;\n\t\t\t");

            for(int counter = 0; counter &lt; contents.size(); counter++){
                System.out.println(contents.get(counter));
                writer.write("\t\t\t&lt;li&gt; " + contents.get(counter) + " &lt;/li&gt;\n");
            }
            writer.write("\t\t&lt;/ul&gt;\n\t&lt;/body&gt;\n&lt;/html&gt;");
        }
    }
    public static void main(String[] args){
        try(Scanner userInput = new Scanner(System.in)){
            File testInput;
            do{
                System.out.print("Please enter the name of the directory: ");
                String input = userInput.next();
                testInput = new File(input);
            }
            while(!testInput.isDirectory());
            writeDirectoryFilesToHTML(testInput);
        }
        catch(IOException io_exc){
            System.out.println("An error occurred: " + io_exc.getMessage());
        }
    }
}
Files and Paths

Since Java 7, the recommended way of traversing a file system is to rely on the new Path class and associated Files and Paths helper classes.

Furthermore, since you are on Java 8, there is the Files.list(Path) method that lets you iterate through a given directory as a form of Stream&lt;Path&gt;, so that you can leverage on the stream-processing features.

For example, just to pad each entry in the given directory with &lt;li&gt;...&lt;/li&gt;, the following snippet does something similar to your for-loop:

List&lt;String&gt; contents;
try {
    // assuming directoryPath is validated as a directory already
    contents = Files.list(directoryPath)
                    .map(v -&gt; "&lt;li&gt;" + v + "&lt;/li&gt;")
                    .collect(Collectors.toList());
} catch (IOException e) {
    // handle accordingly
}


To write contents out to a file, there's also Files.write(Path, Iterable, OpenOption):

try {
    Files.write(outputPath, contents);
} catch (IOException e) {
    // handle accordingly
}


However, since you need to write the header and 'close' the end of the HTML document, you can rely on the forEach() terminal operation of a Stream:

private static void writeDirectoryFilesToHTML(Path directoryPath, 
                                                Path outputPath) throws IOException {
    try (FileWriter target = new FileWriter(outputPath.toFile());
            BufferedWriter buffered = new BufferedWriter(target);
            PrintWriter writer = new PrintWriter(buffered)) {
        writer.write("&lt;html&gt;\n\t&lt;body&gt;\n\t\t&lt;h1&gt;Contents of "
                        + directoryPath.toRealPath() 
                        + ":&lt;/h1&gt;\n\t\t&lt;ul&gt;\n");
        Files.list(directoryPath)
                .map(v -&gt; "\t\t\t&lt;li&gt; " + v + " &lt;/li&gt;")
                .forEach(writer::println);
        writer.write("\t\t&lt;/ul&gt;\n\t&lt;/body&gt;\n&lt;/html&gt;");
    }
}


Here, writer::println is used as a method reference.

HTML output and OS-dependent newlines

You can also consider using some sort of a HTML output library so that you do not have to manually specify "&lt;html&gt;...&lt;ul&gt;...&lt;/ul&gt;...&lt;/html&gt;" (for example).

You are currently hard-coding your newlines to be \n, so you may want to create a helper method that writes the OS-dependent newlines for you, after every call to write().

Other observations

As you can see from above, it's recommended that your method takes in the output path as well, instead of assuming "output.html".