Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro JSP
|
Pro JSP Advanced JSP coding questions. Beginning questions will be redirected to the Beginning JSP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro JSP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 6th, 2004, 03:53 PM
Registered User
 
Join Date: Dec 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to starsailor
Default 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");
    }
  }
}














Similar Threads
Thread Thread Starter Forum Replies Last Post
write to file angelboy C# 2005 4 March 25th, 2007 05:01 AM
write file abaso.jadhav01 ASP.NET 2.0 Basics 0 November 24th, 2006 06:15 PM
program won't write to file CNewbie Visual C++ 1 December 7th, 2004 06:20 PM
reading HTML from file -> write to file mikeuk Beginning PHP 4 July 21st, 2004 05:40 AM
return previous file after a window is closed hosefo81 PHP How-To 2 January 1st, 2004 02:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.