in Education by
I have a text file, by which I am trying to write to in my program. Whenever user wants to add a book to a library catalog, the book details will be saved to the text file. The problem is that my program successfully writes to the text file, but it only overwrites the first line over and over again if I try to add another book. I have an ArrayList that inherits from a Book class, which contains the specific attributes of a book (title, author, publisher,etc.) In my current program, I have created a variable called bookInfo that stores the String and int values of the book attributes from their respective jTextFields. I believe that it is a "class variable" (but I may be wrong) because I initialize it to the book class's constructor Book bookInfo = new Book(bookTitle, author, publisher, brandCallNumber, numberOfCopies); This variable is then added to the arrayList, and I call the WriteToFile() method in my program. The code for the AddBookDialog class public class AddBookDialog extends javax.swing.JDialog { ArrayList books = new ArrayList(); /** * Creates new form AddBook */ public AddBookDialog(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); } private void jButtonSaveToLibraryActionPerformed(java.awt.event.ActionEvent evt) { String bookTitle = jTextFieldTitle.getText(); String author = jTextFieldAuthor.getText(); String publisher = jTextFieldPublisher.getText(); String brandCallNumber = jTextFieldBranchCallNumber.getText(); int numberOfCopies = Integer.parseInt(jTextFieldCopies.getText()); Book bookInfo = new Book(bookTitle, author, publisher, brandCallNumber, numberOfCopies); books.add(bookInfo); WriteToFile(); this.setVisible(false); } private void WriteToFile() { WriteFile wf = new WriteFile("BookDetails.txt"); for(int i = 0; i < books.size(); i++) { Book b = books.get(i); String bookDetails = b.getBookTitle() + " : " + b.getAuthorName() + " : " + b.getPublisherName() + " : " + b.getBranchCallNumber() + " : " + b.getNumberOfCopies(); wf.write(bookDetails); } wf.close(); } Code for WriteFile class public class WriteFile{ private FileOutputStream filename; public WriteFile(String inputfilename) { try { filename = new FileOutputStream(inputfilename,false); } catch (IOException e) {} } public void write(String item) { PrintStream output = new PrintStream(filename); output.println(item); } public void close() { try { filename.close(); } catch (IOException e) { e.printStackTrace(); } } } What I expect my program to do is to continuously add on to the text file. If I add another book, then the text file's contents should consist of Harry Potter : JK Rowling : Scholastic : FIC Rowl : 14 Hamlet : William Shakespeare : Penguin : FIC Shak : 23 Instead, when I try to write another book to the file, it instead overwrites the first line Hamlet : William Shakespeare : Penguin : FIC Shak : 23 //The book details for Harry Potter are overwritten JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
According to Javadoc: public FileOutputStream(File file, boolean append) throws FileNotFoundException Creates a file output stream to write to the file represented by the specified File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection. The second argument (boolean append) is used to control whether to append to existing file instead of override it. So in your case, you should pass true as your second argument

Related questions

0 votes
    I've trained a sentiment classifier model using Keras library by following the below steps(broadly). Convert Text ... around this? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I think I shall reframe my question from Where should you use BlockingQueue Implementations instead of Simple Queue ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    I want to post a message on slack using allbegrey Slack API . The message contains links. How to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I want to post a message on slack using allbegrey Slack API . The message contains links. How to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    Which of these package is used for text formatting in Java programming language? (a) java.text (b) java. ... Expressions of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    How can we take input text from user in HTML page? (a) input tag (b) inoutBufferedReader tag (c) meta ... & Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of the following is correct error when loading JAR file with duplicate name? (a) java.io.NullPointerException ( ... JDBC of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    I have an Enum package com.javarnd.bbms.enums; public enum BloodTypeEnum { A_PLUS(1, "A+"), ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    I create a plot_ly image using: MilesPlotly...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I create a plot_ly image using: MilesPlotly...
asked Jul 6, 2022 in Education by JackTerrance
0 votes
    I am trying to do some validation on the file name before it's saved. Here are my three file- ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    ____is the default File Extension for saving presentation in Power point 2013. Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    What are the correct extensions for saving an HTML file? 1. .hml 2. .xml 3. .html 4. .htm 5. option a and c 6. option c and d...
asked Jul 12, 2021 in Technology by JackTerrance
0 votes
    first..sorry for my poor english and my big noob question..the problem is... I need create a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Consider two given file paths as strings: /path/to/dir and /path/to/dir/file In Java, how can ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
...