1 package at.ac.tuwien.lsdc.util;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.util.LinkedList;
9 import at.ac.tuwien.lsdc.types.Application;
10 import au.com.bytecode.opencsv.CSVReader;
11 import au.com.bytecode.opencsv.CSVWriter;
13 public class CSVParser {
14 public static LinkedList<Application> parseFile(File csvFile)
16 CSVReader reader = new CSVReader(new FileReader(csvFile), ';',
17 CSVWriter.NO_QUOTE_CHARACTER);
18 List<String[]> file = reader.readAll();
19 LinkedList<Application> apps = new LinkedList<Application>();
20 // start at 1 to ignore header
21 for (int i = 1; i < file.size(); i++) {
22 String[] line = file.get(i);
23 if (line.length != 5) {
25 throw new IOException(
26 "CSVParser: Not right amount of elements in line " + i
30 Application a = parseLine(line);
32 } catch (NumberFormatException e) {
34 throw new IOException("Couldn't parse line");
42 private static Application parseLine(String[] appStr)
43 throws NumberFormatException {
44 long timestamp = Long.parseLong(appStr[0]);
45 int size = Integer.parseInt(appStr[1]);
46 int ram = Integer.parseInt(appStr[2]);
47 int cpu = Integer.parseInt(appStr[3]);
48 int duration = Integer.parseInt(appStr[4]);
49 Application app = new Application(timestamp, size, ram, cpu, duration);