import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class FileHandler implements iData { public List tryGetEmployees() throws FileNotFoundException { List dolgozok = new ArrayList<>(); File file = new File("surubt.txt"); Scanner scanner = new Scanner(file); while(scanner.hasNext()) { String line = scanner.nextLine(); String[] lineArray = line.split(":"); Dolgozo dolgozo = new Dolgozo(); dolgozo.az = Integer.parseInt(lineArray[0]); dolgozo.nev = lineArray[1]; dolgozo.telepules = lineArray[2]; dolgozo.fizetes = Double.parseDouble(lineArray[3]); dolgozok.add(dolgozo); } return dolgozok; } public List getEmployees() { List dolgozok = new ArrayList<>(); try { dolgozok = tryGetEmployees(); } catch (FileNotFoundException e) { System.err.println("Hiba! A fájl nem található!"); } return dolgozok; } public void tryInsertEmployee(String name, String city) throws IOException { FileWriter fileWriter = new FileWriter("surubt.txt", true); PrintWriter printWriter = new PrintWriter(fileWriter); StringBuilder data = new StringBuilder(); data.append("1:"); data.append(name + ":"); data.append(city + ":0"); printWriter.println(data.toString()); printWriter.close(); } public void insertEmployee(String name, String city) { try { tryInsertEmployee(name, city); } catch (IOException ex) { System.err.println("Hiba! A fájl írása sikertelen!"); System.err.println(ex.getStackTrace()); } } }