Sitemap Creator Java Exercise

Exercise

Sitemap Creator

Objetive

A "sitemap" is a file that webmasters can use to inform Google about the webpages that their website includes, with the aim of achieving better search engine rankings.

You must create a program that displays the contents of a preliminary "sitemap" on the screen. The sitemap should be generated from the list of ".html" files in the current folder, with a "weekly" frequency and the current date as the "last modification" date.

Example Code

import java.util.*;
public class Main
{
	static void main(String[] args)
	{
		ArrayList ListHtml = GetHtml();

		CreateSiteMap(ListHtml, "weekly", java.time.LocalDateTime.now());
	}

	private static void CreateSiteMap(ArrayList listHtml, String frecuency, java.time.LocalDateTime lastUpdated)
	{
		try
		{
			java.io.FileWriter writer = new java.io.OutputStreamWriter(File.Create("sitemap.xml"));

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

			for (String html : listHtml)
			{
				writer.write("" + System.lineSeparator());
				writer.write("" + html + "" + System.lineSeparator());
				writer.write("" + lastUpdated.ToShortDateString() + "" + System.lineSeparator());
				writer.write("" + frecuency + "" + System.lineSeparator());
				writer.write("" + System.lineSeparator());
			}

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

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

	private static ArrayList GetHtml()
	{
		ArrayList ListHtml = 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 ".html":
				case ".htm":
					ListHtml.add(file.substring(2));
					break;
			}
		}

		return ListHtml;
	}
}

More Java Exercises of Additional Libraries