I am having trouble appending an arraylist to an existing textfile, it appends but in the process clears all of the existing data inside of the file. Please can you help :) many thanks in advance

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);
    }

            try { 
        FileWriter fw = new FileWriter("Stock.txt", true);
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter out = new PrintWriter(bw);

        for (Books newBook: details) {
            out.write(newBook.toString() + "\n");
        }
    } catch (IOException e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.