List of images as HTML Java Exercise - Java Programming Course

 Exercise

List of images as HTML

 Objetive

Create a program that creates an HTML file that lists all the images (PNG and JPG) in the current folder.

For instance, if the current folder contains the following images:

1.png
2.jpg

 Example 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;
	}
}


Juan A. Ripoll - Programming Tutorials and Courses © 2024 All rights reserved.  Legal Conditions.