why does this over write the previous file
can anybody please help
import java.awt.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Assessement3 extends JFrame implements ActionListener{
JLabel Title,Photo,Topic,Author,Quote;
JTextField AuthorText,TopicText;
JButton QuoteButton;
JTextArea QuoteArea;//various variables that i needed to complete the program
//ArrayList Author= new ArrayList();
int users;
//JLabel Author, Topic;
ArrayList theauthor = new ArrayList();
ArrayList thesubject = new ArrayList();
ArrayList thequote = new ArrayList();
public Assessement3(){//this method is used to organise the interface
Container c =getContentPane();
c.setLayout(null);
Title=new JLabel("Quote Generator Administrator", JLabel.CENTER);
Title.setFont(new Font("Serif", Font.BOLD, 24));
Title.setForeground(Color.white);
Title.setBounds(0,5,350,30);
c.add(Title);
//Photo=new JLabel(candle.gif);
//private Image candle;
Author=new JLabel("Authors Name");
Author.setForeground(Color.white);
Author.setBounds(10,40,150,20);
c.add(Author);
AuthorText=new JTextField();
AuthorText.setBounds(10,60,150,25);
c.add(AuthorText);
Topic=new JLabel("Topic Area");
Topic.setForeground(Color.white);
Topic.setBounds(190,40,150,25);
c.add(Topic);
TopicText=new JTextField();
TopicText.setBounds(190,60,150,25);
c.add(TopicText);
Quote=new JLabel("Please Enter the Quote", JLabel.CENTER);
Quote.setForeground(Color.white);
Quote.setBounds(0,100,350,25);
c.add(Quote);
QuoteArea=new JTextArea();
QuoteArea.setBounds(30,130,290,130);
c.add(QuoteArea);
QuoteButton=new JButton("Press Here to Enter the Quote into the System");
QuoteButton.setBounds(2,270,348,30);
QuoteButton.setForeground(Color.white);
c.add(QuoteButton);
QuoteButton.addActionListener(this);
setBackground ( Color.black );
}
public static void main(String args[]){
Assessement3 mainWindow;
mainWindow= new Assessement3();
mainWindow.setSize(350,300);
mainWindow.setVisible(true);
}
public void actionPerformed (ActionEvent e){
if(e.getSource() ==QuoteButton){//adds the data to the java consol
writeThefile();
}
} public void writeThefile() {
String str, tmp;
str="";
loadQuoteFile();
try {
FileWriter basewriter = new FileWriter ("Data.txt");
BufferedWriter buffer = new BufferedWriter (basewriter);
PrintWriter out = new PrintWriter (buffer);
out.write ("\n"+AuthorText.getText()+"\n"+TopicText.getText( )+"\n"+QuoteArea.getText()+"++++++++");
//out.close();
for (int i = 0; i < thequote.size(); i++) {
out.write ("\n"+theauthor.get(i)+"\n"+thesubject.get(i)+"\n" +thequote.get(i)+"\n++++++++");
}
//out.write ("\n"+AuthorText.getText()+"\n"+TopicText.getText( )+"\n"+QuoteArea.getText()+"++++++++");
out.close();
} catch (IOException except) {
JOptionPane.showMessageDialog (null, "The File was not readable");
}
}
public void loadQuoteFile() {
try {
// Open the file that stores my quotes
FileReader quoteStream = new FileReader ("Data.txt");
BufferedReader quoteIn = new BufferedReader (quoteStream);
String str, tmp;
boolean finishedQuote = false;
int readfileCount = 0;
tmp = "";
while ((str = quoteIn.readLine()) != null) {
if (str.startsWith("+")) { finishedQuote = true; readfileCount = 0; }
if (!str.startsWith("+")) { finishedQuote = false; readfileCount++; }
if (readfileCount == 1) {
theauthor.add(str);
} else if (readfileCount == 2) {
thesubject.add(str);
} else {
if (finishedQuote) {
thequote.add(tmp);
tmp = "";
} else {
tmp = tmp+"\n"+str;
}
}
}
// Close the File
quoteIn.close();
} catch (IOException except) {
JOptionPane.showMessageDialog (null, "The File was not readable");
}
}
}
|