import java.io.IOException;
import java.io.FileWriter;
import java.io.File;
import java.net.URL;
import java.util.Scanner;

public class converter
{
    public static final String DIRECTOR = "Director:";
    public static final String STARRING = "Starring:";
    public static final String RATING = "Rating:";
    public static final String MEDIA = "Media:";
    public static final String LANGUAGES = "Languages:";
    public static final String GENRE = "Genre:";

    public static final String OUTPUT_FILE = "library.csv";

    public static void main (String args[]) throws IOException
    {
	String path = ".";

	for (int i = 0; i < args.length; i++)
	{
	    if ("-help".equals(args[i]))
	    {
		System.out.println("converter [-path <relative path>] [-help]");
		System.exit(0);
	    }

	    if ("-path".equals(args[i]))
		path = args[i+1];
	}

	FileWriter csvWriter = new FileWriter(OUTPUT_FILE);

	File folder = new File(System.getProperty("user.dir")+"/"+path);
	File[] fileNames = folder.listFiles();

	for (File file : fileNames)
	{
	    URL url = new URL("file://"+file.getPath());

	    addFileData(url, csvWriter);
	}

	csvWriter.flush();
	csvWriter.close();
    }

    private static void addFileData (URL url, FileWriter csvWriter) throws IOException
    {
	Scanner sc = new Scanner(url.openStream());
	StringBuffer sb = new StringBuffer();

	while (sc.hasNext())
	{
	    sb.append(sc.next());
	}

	String result = sb.toString();

	// Removing the HTML tags

	result = result.replaceAll("<[^>]*>", "");
	System.out.println("Contents of the web page: "+result);

	// sometimes genre is there before Director

	int genreStart = result.indexOf(converter.GENRE);
	String movie = ((genreStart == -1) ? "" : result.substring(0, genreStart));

	int directorStart = result.indexOf(converter.DIRECTOR);
	if ("".equals(movie))
	{
	    movie = result.substring(0, directorStart);
	}

	int starsStart = result.indexOf(converter.STARRING);
	String director = ((starsStart == -1) ? "" : result.substring(directorStart+converter.DIRECTOR.length(), starsStart));

	int mediaStart = result.indexOf(converter.MEDIA);
	int languagesStart = result.indexOf(converter.LANGUAGES);
	String media = ((languagesStart == -1) ? "" : result.substring(mediaStart+converter.MEDIA.length(), languagesStart));

	/*
	System.out.println("Movie: "+movie);
	System.out.println("Director: "+director);
	System.out.println("Media: "+media);
	*/

	csvWriter.append(movie);
	csvWriter.append(",");
	csvWriter.append(director);
	csvWriter.append(",");
	csvWriter.append(media);
	csvWriter.append("\n");
    }

}
@@ -150,55 +150,263 @@
     
-public static final String DIRECTOR = %22Director
+/**%0A     * The various fields representing the movie. Apart from the movie title, all%0A     * of them are optional.%0A     *%0A     * DO NOT re-order.%0A     */%0A%0A    public static final String DIRECTOR = %22Director:%22;%0A    public static final String GENRE = %22Genre
 :%22;%0A
@@ -620,57 +620,8 @@
 s:%22;
-%0A    public static final String GENRE = %22Genre:%22;
 %0A%0A  
@@ -1832,24 +1832,89 @@
 );%0A%09
-String movie = (
+int directorStart = result.indexOf(converter.DIRECTOR);%0A%09String genre = %22%22;%0A%0A%09if 
 (gen
@@ -1917,37 +1917,43 @@
 (genreStart 
-=
+!
 = -1)
- ? %22%22 :
+%0A%09    genre =
  result.subs
@@ -1958,19 +1958,16 @@
 bstring(
-0, 
 genreSta
@@ -1972,70 +1972,130 @@
 tart
-));%0A%0A%09int director
++converter.GENRE.length(), directorStart);%0A%0A%09String movie = ((genre
 Start =
- result.indexOf(converter.DIRECTOR
+= -1) ? %22%22 : result.substring(0, genreStart)
 );%0A
+%0A
 %09if 
@@ -2128,16 +2128,46 @@
  movie =
+ ((directorStart == -1) ? %22%22 :
  result.
@@ -2193,16 +2193,17 @@
 orStart)
+)
 ;%0A%09%7D%0A%0A%09i
@@ -2609,12 +2609,8 @@
 );%0A%0A
-%09/*%0A
 %09Sys
@@ -2643,16 +2643,54 @@
 movie);%0A
+%09System.out.println(%22Genre: %22+genre);%0A
 %09System.
@@ -2766,12 +2766,8 @@
 ia);
-%0A%09*/
 %0A%0A%09c

Update converter.java