title: How do i write to a text file using java?
body: <p>I have been using the "Learning Java 2nd Edtion" book to try make my java application write my input to a text file called properties. I have manipulated the text book example into my own code but still having problems trying to get it to work. I think i may need to hook it up to my submit button but this wasnt mentioned within the chapter.</p>
  
  <p>Basically im trying to store the information in the text file and then use that text file within another location to read all of the property details.</p>
  
  <p>Here is my code for the AddProperty Page so far any advice would be greatly appreciated. 
  At the moment iv hit the wall.</p>
  
  <pre><code>/**
   *
   * @author Graeme
   */
  package Login;
  
  import java.io.*;
  
  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.border.EmptyBorder;
  
  public class AddProperty
  {
  public void AddProperty()
  {
  
      JFrame frame = new JFrame("AddPropertyFrame");
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      // having to set sizes of components is rare, and often a sign    
      // of problems with layouts.
      //frame.setSize(800,600);
      JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20,20));
      // make it big like the original
      panel.setBorder(new EmptyBorder(100,20,100,20));
      frame.add(panel);
      //panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
  
      JLabel HouseNumber = new JLabel("House Number/Name");
      panel.add(HouseNumber);
      JTextField HouseNumber1 = new JTextField(10);
      panel.add(HouseNumber1);
  
      JLabel HousePrice = new JLabel("House Price");
      panel.add(HousePrice);
      JTextField HousePrice1 = new JTextField(10);
      panel.add(HousePrice1);
  
      JLabel HouseType = new JLabel("House Type");
      panel.add(HouseType);
      JTextField HouseType1 = new JTextField(10);
      panel.add(HouseType1);
  
      JLabel Location = new JLabel("Location");
      panel.add(Location);
      JTextField Location1 = new JTextField(10);
      panel.add(Location1);
  
      JButton submit = new JButton("Submit");
      panel.add(submit);
      submit.addActionListener(new Action());
  
      // tell the GUI to assume its natural (minimum) size.
      frame.pack();
  }
  
  static class Action implements ActionListener{
  
      @Override
      public void actionPerformed (ActionEvent e)
      {
          // this should probably be a modal JDialog or JOptionPane.
         JOptionPane.showMessageDialog(null, "You have successfully submitted a property.");
      }
  
      static class propertyList 
      {
          public static void main (String args[]) throws Exception {
              File properties = new File(args[0]);
  
              if (!properties.exists() || !properties.canRead() ) {
                  System.out.println("Cant read " + properties);
                  return;
              }
              if (properties.isDirectory()){
                  String [] properties1 = properties.list();
                  for (int i=0; i&lt; properties1.length; i++)
                      System.out.println();
              }
              else
                  try {
                      FileReader fr = new FileReader (properties);
                      BufferedReader in = new BufferedReader (fr);
                      String line;
                      while ((line = in.readLine())!= null)
                      System.out.println(line);
                  }
              catch (FileNotFoundException e){
                  System.out.println("Not Able To Find File");
              }
          }
      }
  }
  </code></pre>
  
  <p>}</p>