Как добавить текст в конец файла? Java
Как сделать так, чтобы после каждого моего ввода данных файл не перезаписывался, а продолжал добавлять данные ниже? Код:
public static void main(String[] args) throws IOException, ClassNotFoundException {
List<ForClient> existsClients = read();
existsClients.add(createClients());
write(existsClients);
List<ForClient> clients = read();
System.out.println(clients);
}
public class ForClient extends CarData implements Serializable {
private String fullName;
private String email;
private String country;
private String phoneNumber;
public ForClient() {
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Override
public String toString() {
return "Client [clientFullname=" + getFullName()
+ ", clientPhoneNumber=" + getPhoneNumber()
+ ", clientEmail=" + getEmail()
+ ", clientCountry=" + getCountry() + "]";
}
}
Ответы (1 шт):
Автор решения: Дмитрий
→ Ссылка
У вас чтение/запись заточена на один объект, а вы хотите иметь возможность добавлять, следовательно, нужно иметь возможность сохранять множество объектов.
public class Program {
private final static String FILE_NAME = "1.txt";
private final static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException, ClassNotFoundException {
List<ForClient> existsClients = read();
existsClients.add(createClients());
write(existsClients);
List<ForClient> clients = filter(read(),
(client1, client2) -> {
return client1.getFullName().compareTo(client2.getFullName());
},
client -> {
//return client.getFullName().equals("some_name");
return true;
});
System.out.println(clients);
}
public static List<ForClient> filter(List<ForClient> clients,
Comparator<ForClient> comparator, Predicate<ForClient> predicate) {
return clients.stream()
.filter(predicate)
.sorted(comparator)
.collect(Collectors.toList());
}
public static ForClient createClients() throws IOException, ClassNotFoundException {
ForClient client = new ForClient();
System.out.println("Enter your fullname:");
client.setFullName(scanner.nextLine());
System.out.println("Enter your phone number:");
client.setPhoneNumber(scanner.nextLine());
return client;
}
public static void write(List list) throws IOException, ClassNotFoundException {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
oos.writeObject(list);
}
}
public static List read() throws IOException, ClassNotFoundException {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
return (List) ois.readObject();
} catch (EOFException e) {
return new ArrayList<>();
}
}
}