Please could someone help me with this issue, I am trying to get append a new book to a CSV file from user input but it appends to the file and clears the existing data within the file. How do I append the data to the bottom of the file without deleting the original data?
Code Scanner myScanner = new Scanner (System.in);
ArrayList<Books> details = new ArrayList<Books>();
int ISBN;
String bookType;
String title;
String language;
String genre;
String releaseDate;
float price;
float quantity;
float addInfo1;
String addInfo2;
System.out.println("ISBN:");
ISBN = Integer.parseInt(myScanner.nextLine());
System.out.println("Book Type:");
bookType = myScanner.nextLine();
System.out.println("Title:");
title = myScanner.nextLine();
System.out.println("Language:");
language = myScanner.nextLine();
System.out.println("Genre:");
genre = myScanner.nextLine();
System.out.println("Release date:");
releaseDate = myScanner.nextLine();
System.out.println("Price:");
price = Float.parseFloat(myScanner.nextLine());
System.out.println("Quantity:");
quantity = Float.parseFloat(myScanner.nextLine());
System.out.println("AddInfo1:");
addInfo1 = Float.parseFloat(myScanner.nextLine());
System.out.println("Addnfo2:");
addInfo2 = myScanner.nextLine();
details.add(new Books(ISBN, bookType, title, language, genre, releaseDate, price, quantity, addInfo1, addInfo2));
for (Books newBook : details) {
System.out.println(newBook);
BufferedWriter bw = null;
try {
bw = new BufferedWriter (new FileWriter("Stock.txt", false));
for (Books newBook1 : details) {
bw.write(newBook1.toString() + "\n");
}
} catch (IOException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException e) {
System.err.println(e.getMessage());
}
}