private void createNewElement(Manager manager) { try { scanner = new Scanner(file); String partString = ""; String newString = ""; String readString = ""; String writeString = ""; int i = 0; while (scanner.hasNextLine()) { if (i++ == 0) { partString = scanner.nextLine(); readString += partString + "\n"; } else { readString += scanner.nextLine() + "\n"; } } int id = Integer.parseInt(partString.split(":")[1]) + 1; newString += "counter:" + id; String str = readString.replaceAll(partString, newString); writeString += str + "id:" + id + '\n'; writeString += "Name:" + manager.getName() + '\n'; writeString += "Surname:" + manager.getSurname() + '\n' + "Age:" + manager.getAge() + '\n'; writeString += "IDdepartment:" + manager.getDepartment().getIdDepartment(); fileOutputStream = new FileOutputStream(file); printWriter = new PrintWriter(fileOutputStream); printWriter.println(writeString); printWriter.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
private void createNewFile(Manager manager) { try { fileOutputStream = new FileOutputStream(file, true); printWriter = new PrintWriter(file); printWriter.println("counter:1"); printWriter.println("id:1"); printWriter.println("Name:" + manager.getName()); printWriter.println("Surname:" + manager.getSurname()); printWriter.println("Age:" + manager.getAge()); printWriter.println("IDdepartment:" + manager.getDepartment().getIdDepartment()); printWriter.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } }