0

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());
            }
        }
  • I know there is no validation but I want to get the basic code finished and working before adding stuff such as this :) – Dan Rogers May 12 '21 at 12:31
  • https://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java check if this works. – Mayur May 12 '21 at 18:03

2 Answers2

2

You'll need to open file for writing in append mode. For the FileWriter should be new FileWriter("Stock.txt", true).

The second argument for FileWriter should be true instead of false

0

the posted code already has the answer, last lines of that part:

bw = new BufferedWriter (new FileWriter("Stock.txt", false));
// False here means that we don't want to append data to the content of the file.
// If we need to append, then we should use true.