List of images as HTML Learn programming Java

Lesson:

Additional Libraries


Exercise:

List of images as HTML 40


Objetive:

Create a program to create an HTML file containing the list of images (PNG and JPG) in the current directory.

For example, in the current directory there are images called.

1.png
2.jpg


Code:

import java.util.*;
public class Main
{
	static void main(String[] args)
	{
		CreateHtml(GetImages());
	}

	private static void CreateHtml(ArrayList listImages)
	{
		try
		{
			java.io.FileWriter writer = new java.io.OutputStreamWriter(File.Create("images.html"));

			writer.write("" + System.lineSeparator());
			writer.write("" + System.lineSeparator());

			for (String image : listImages)
			{
				writer.write("" + image + "" + System.lineSeparator());

			}

			writer.close();
		}
		catch (java.lang.Exception e)
		{
			System.out.println("Error writing html.");
		}
	}

	private static ArrayList GetImages()
	{
		ArrayList ListImages = new List();

		String[] files = (new java.io.File(".")).list(java.io.File::isFile);

		for (String file : files)
		{
			String extension = Path.GetExtension(file);

			switch (extension)
			{
				case ".png":
				case ".jpg":
				case ".jpge":
					ListImages.add(file.substring(2));
					break;
			}
		}

		return ListImages;
	}
}