Commit 44e088db by Patryk Czarnik

ObslugaCSV

parent 863e48dc
package emps; package emps;
import java.io.File;
import java.io.FileNotFoundException;
import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Scanner;
// Klasa narzędziowa / utility class
// Jest to zestaw metod statycznych. Jak moduł w Pythonie czy C++.
public class ObslugaCSV { public class ObslugaCSV {
public List<Employee> wczytaj(String sciezka) { public static List<Employee> wczytaj(String sciezka) {
List<Employee> emps = new ArrayList<>(); List<Employee> emps = new ArrayList<>();
// TODO // try with resources - od Java 7 - automatyczne zamykanie plików i innych zasobów
try(Scanner scanner = new Scanner(new File(sciezka))) {
scanner.nextLine(); // pomijamy pierwszą linię z nagłówkami
while(scanner.hasNextLine()) {
String linia = scanner.nextLine();
String[] t = linia.split(";", -1);
Employee emp = new Employee(Integer.parseInt(t[0]), t[1], t[2], t[3],
Integer.parseInt(t[4]), LocalDate.parse(t[5]), t[6], t[7], t[8], t[9], t[10]);
emps.add(emp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return emps; return emps;
} }
......
...@@ -5,9 +5,7 @@ import java.util.List; ...@@ -5,9 +5,7 @@ import java.util.List;
public class TestOdczytu { public class TestOdczytu {
public static void main(String[] args) { public static void main(String[] args) {
ObslugaCSV obiekt = new ObslugaCSV(); List<Employee> emps = ObslugaCSV.wczytaj("emps.csv");
List<Employee> emps = obiekt.wczytaj("emps.csv");
System.out.println("Wczytano " + emps.size() + " rekordów"); System.out.println("Wczytano " + emps.size() + " rekordów");
for (Employee emp : emps) { for (Employee emp : emps) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment