public static void main(String[] args) throws Exception {

		CriaBaseSongs sse = new CriaBaseSongs();

		String inputFileName = args[0];
		String outputFileName = args[1];
		String errorFileName = args[2];

		Scanner input = new Scanner(new File(inputFileName));
		int lineNumber = 0;

		if(new File("contagem.txt").exists()){
			lineNumber = new Scanner(new File("contagem.txt")).nextInt();
		}

		int currentLine = 0;
		while(currentLine < lineNumber){
			if(!input.hasNextLine()){
				input.close();
				throw new RuntimeException("Error skipping lines from input file!");
			}
			currentLine++;
			System.err.println("Skipped line (" + currentLine + "): " + input.nextLine());
		}

		while (input.hasNextLine()) {
			String newLine = input.nextLine();
			String[] splittedLine = newLine.split("\t");

			for (int i = 0; i < splittedLine.length; i++) {
				splittedLine[i] = splittedLine[i].trim();
			}

			String outputLine = "";

			boolean coletou = false;

			while(!coletou){
				try {
					outputLine = sse.searchSongByTitleAndArtist(
							splittedLine[2], splittedLine[1], splittedLine[3]);
					coletou = true;
				} catch (Exception e) {
					e.printStackTrace();
					sse.getProximaAPIKey();
				}

			}

			if (outputLine.isEmpty()) {
				FileWriter writer = new FileWriter(new File(errorFileName),
						true);
				writer.write(newLine + "\n");
				writer.close();
			} else {
				FileWriter writer = new FileWriter(
						new File(outputFileName), true);
				StringBuilder sb = new StringBuilder();
				sb.append(splittedLine[0] + "\t");
				sb.append(splittedLine[1] + "\t");
				sb.append(splittedLine[2] + "\t");
				sb.append(splittedLine[3] + "\t");
				sb.append(outputLine + "\t");
				sb.append(splittedLine[4] + "\n");

				writer.write(sb.toString());
				writer.close();
			}

			FileWriter writer = new FileWriter(new File("contagem.txt"),
					false);
			writer.write("" + (++currentLine));
			writer.close();
		}

		input.close();
	}