package ro.dcsi.internship;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.experimental.theories.Theories;

public class CsvExporter2 {
	CsvExporter2() {
		System.out.println("csv exporter called");
	}

	public void export(String inputFileName, String outputFileName) throws IOException {
		FileReader fileReader = new FileReader(inputFileName);
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		FileWriter fileWriter = new FileWriter(outputFileName);
		String s;
		try {
			while ((s = bufferedReader.readLine()) != null) {
				fileWriter.append(s + " \n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		fileReader.close();
		fileWriter.close();
	}

	public String readHeading(String fileName) {
		ArrayList<User> usersArray = new ArrayList<User>();
		String s = new String();
		FileReader fr = null;
		try {
			fr = new FileReader(fileName);
			BufferedReader br = new BufferedReader(fr);
			s = br.readLine();
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			if(fr!=null)
				try {
					fr.close();
				} catch (IOException e) {
					//bug swallowed original exception
					throw new RuntimeException(e);
				}
		}
		return s;
	}

	public List<User> readUsers(String fileName) {
		ArrayList<User> userArray = new ArrayList<User>();
		try {
			FileReader fileReader = new FileReader(fileName);
			BufferedReader bufferedReader = new BufferedReader(fileReader);
			String s;
			String[] splited;
			while ((s = bufferedReader.readLine()) != null) {
				splited = s.split(",");
				userArray.add(new User(splited[0], splited[1], splited[2]));
			}
			return userArray;
		} catch (IOException e) {
			e.printStackTrace();
			return new ArrayList<User>();
		}
	}

}

@@ -294,58 +294,8 @@
 r;%0A%0A
-import org.junit.experimental.theories.Theories;%0A%0A
 publ
@@ -993,24 +993,28 @@
 String();%0A%09%09
+try(
 FileReader f
@@ -1020,30 +1020,8 @@
 fr =
- null;%0A%09%09try %7B%0A%09%09%09fr =
  new
@@ -1033,33 +1033,35 @@
 Reader(fileName)
-;
+) %7B
 %0A%09%09%09BufferedRead
@@ -1206,32 +1206,32 @@
 OException e) %7B%0A
+
 %09%09%09throw new Run
@@ -1252,177 +1252,8 @@
 e);%0A
-%09%09%7D finally %7B%0A%09%09%09if(fr!=null)%0A%09%09%09%09try %7B%0A%09%09%09%09%09fr.close();%0A%09%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09%09//bug swallowed original exception%0A%09%09%09%09%09throw new RuntimeException(e);%0A%09%09%09%09%7D%0A
 %09%09%7D%0A

use try-with-resources java7 specific