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)