package mmaplayers.fightersearch.controller;
|
|
|
|
import mmaplayers.mmaplayers.model.Fighter;
|
|
import org.springframework.util.ResourceUtils;
|
|
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Paths;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class CsvParser {
|
|
|
|
public static List<Fighter> readFile(final String path, final String separator, final String listSeparator) {
|
|
List<Fighter> result = new ArrayList<>();
|
|
|
|
try {
|
|
result = Files
|
|
.lines(Paths.get(ResourceUtils.getFile(path).toURI()))
|
|
.skip(1)
|
|
.map(line -> {
|
|
String[] values = line.split(separator);
|
|
if (values.length >= 5) {
|
|
return new Fighter(values[0], Arrays.asList(values[1].split(listSeparator)), values[2], values[3], values[4] ,values[5]);
|
|
}).collect(Collectors.toList());
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
System.err.println("Error de lectura del fichero de datos: MMA_DanielKuperus.csv");
|
|
System.exit(-1);
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|