I can not delete the "necessary" lines from a text file. There is a class of the Person model, whose fields are parsed on the command line and, using the add command, are added to the text file. Then, the contents of the file can be viewed by entering the view command. And for deletion, use the remove command. For this team wrote the following logic:
try(BufferedReader reader = new BufferedReader(new FileReader(fileName)); BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))){ while ((reader.readLine()) != null){ people.add(person); } List<Person> filteredList = people.stream().filter(p -> person.getSurname().equals(searchString)).collect(Collectors.toList()); people.removeAll(filteredList); writer.write(person.getName()+ "\r" + person.getSurname() + "\r" + person.getAge() + "\r" + person.getEmail() + "\r\n\r\n"); } catch (IOException e){ throw new RuntimeException(e); } But, instead of deleting what you need, everything else is deleted, and what was going to be deleted is overwritten to a file. Help me understand what I'm doing wrong?
I’ll add some information: Model class:
public class Person implements Serializable { private static final long serialVersionUID = -3763710615439958865L; private static AtomicInteger nextID = new AtomicInteger(0); private int id; private String name; private String surname; private int age; private String email; private Person(int id, String name, String surname, int age, String email) { this.id = id; this.name = name; this.surname = surname; this.age = age; this.email = email; } public Person(String name, String surname, int age, String email) { this(nextID.getAndIncrement(), name, surname, age, email); } private int getId() { return id; } public String getName() { return name; } public String getSurname() { return surname; } public int getAge() { return age; } public String getEmail() { return email; } public static PersonBuilder newBuilder() { return new PersonBuilder(); } @Override public String toString() { return "\n\nID: " + getId() + "\nName: " + getName() + "\nSurname: " + getSurname() + "\nAge: " + getAge() + "\nE_mail: " + getEmail(); }} One of the classes in the Controller package, where the logic for the "remove" command is implemented:
public class DeleteCommand implements Command { private String fileName; private String searchString; private List<Person> people = new ArrayList<>(); private Person person; public DeleteCommand(String fileName, String searchString, Person person) { this.fileName = fileName; this.searchString = searchString; this.person = person; } @Override public void execute(String[] args) { try(BufferedReader reader = new BufferedReader(new FileReader(fileName)); BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))){ while ((reader.readLine()) != null){ people.add(person); } List<Person> filteredList = people.stream().filter(p -> p.getSurname().equals(searchString)).collect(Collectors.toList()); people.removeAll(filteredList); writer.write(person.getName()+ "\r" + pperson.getSurname() + "\r" + person.getAge() + "\r" + person.getEmail() + "\r\n\r\n"); } catch (IOException e){ throw new RuntimeException(e); }}}
personto thepeoplelist by the number of lines in the input file, then perform some manipulations with this list, then write data from thepersonto the output file and finish this (the list does not participate in the record at all). - zRrrperson)writer.write(person.getName()..., because the linewriter.write(person.getName()...writes it there and this is the only line that writes to the file. Reading is also bad, t. because you simply ignore the lines read from the file. The filtering could be replaced withremoveIf, but it is working. - zRrr