Sitemap creator v2 Learn programming Java



Lesson:

Additional Libraries


Exercise:

Sitemap creator v2


Objetive:

You need to create a program that takes the following parameters: the name of a text file containing the URLs, the modification date, and the change frequency.

Example: sitemapCreator urls.txt 2011-11-18 weekly

The text file should contain a list of file names to be indexed, with each file name on a separate line.


Code:

import java.util.*;
public class Main
{
	static void main(String[] param)
	{
		if (param.length != 3)
		{
			System.out.println("Error number of params.");
			return;
		}

		String file = param[0];
		String date = param[1];
		String frecuency = param[2];

		ArrayList ListUrls = GetUrls(file);

		CreateSiteMap(ListUrls, frecuency, date);
	}

	private static void CreateSiteMap(ArrayList listHtml, String frecuency, String 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 + "" + 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 GetUrls(String nameFile)
	{
		try
		{
			java.io.FileReader reader = new java.io.InputStreamReader(File.OpenRead(nameFile));
			java.io.BufferedReader readerBufferedReader = new java.io.BufferedReader(reader);
			String line = "";
			ArrayList urls = new List();

			do
			{
				line = readerBufferedReader.readLine();

				if (line != null)
				{
					urls.add(line);
				}
			} while (line != null);

			reader.close();

			return urls;
		}
		catch (java.lang.Exception e)
		{
			System.out.println("Error reading file.");

			return null;
		}
	}



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