probleming writing multiple items to text file
Hello all,
I am taking a graduate seminar that involves Java. I am working on an assignment where I create an interface that will capture a books title, author, and availability. The end result is to write this to a file 5 times. However, I am finding that each time I press the "save" button on my interface, it writes to the text file but replaces the text that was there before.
Code Java:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class TryThis extends JFrame {
private JPanel panel;
private JButton button;
private JCheckBox check;
private JTextField bookTitle;
private JTextField authorField;
private JLabel bookLabel;
private JLabel authorLabel;
String title;
String author;
String available;
public TryThis(String str) {
super(str);
}
public static void main(String[] args) {
TryThis myGUI = new TryThis("Save book records");
myGUI.createAndShowGUI();
// myGUI.writeTofile();
}
private void createAndShowGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.setLayout(null);
JButton button = new JButton("Save");
button.setBounds(230, 125, 75, 20);
this.add(button);
button.addActionListener(new BookEntry());
check = new JCheckBox("Available", false);
check.setBounds(230, 100, 100, 20);
this.add(check);
bookLabel = new JLabel("Book title:");
bookLabel.setBounds(10, 10, 100, 20);
this.add(bookLabel);
bookTitle = new JTextField();
bookTitle.setBounds(120, 10, 350, 20);
this.add(bookTitle);
authorLabel = new JLabel("Author:");
authorLabel.setBounds(10, 50, 100, 20);
this.add(authorLabel);
authorField = new JTextField();
authorField.setBounds(120, 50, 350, 20);
this.add(authorField);
setSize(500, 200);
setVisible(true);
}
public class BookEntry implements ActionListener {
public void actionPerformed(ActionEvent ev) {
title = bookTitle.getText();
author = authorField.getText();
System.out.println(title);
System.out.println(author);
if (check.isSelected()) {
available = "available";
} else {
available = "not available";
}
System.out.println(available);
StringBuilder result = new StringBuilder();
result.append(title);
result.append(author);
result.append(available);
try { // all the I/O stuff must be a in try/catch
File file = new File("book.txt");
if (!file.exists()) {
file.createNewFile();
System.out.println("new file:" + file);
}
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(result.toString());
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
What am I doing wrong?
Thank you.
Re: probleming writing multiple items to text file
See the API for FileWriter
FileWriter (Java Platform SE 6)
There is a constructor that takes a boolean option which allows you to append to a given file, otherwise it will by default overwrite the file.