开发者

How do i write to a text file using java?

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.

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.

Here is my code for the AddProperty Page so far any advice would be greatly appreciated. At the moment iv hit the wall.

/**
 *
 * @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< 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");
            }
        }
    }
}

}


in your Action performed you are not stating anything, for example in your action performed you could add.

   public void actionPerformed(ActionEvent e)
            {

              houseNumber2 = houseNumber1.getText();
              housePrice2 = housePrice1.getText();
              town1 = town.getText();
              comboBoxType2 = comboBoxType1.getSelectedItem();


            inputData = housenumber2 + "," + housePrice2 + "," + town1 + "," + comboBoxType2;
             FileName.Filewritermethod(inputData);
             frame.setVisible(false);

            }
       });

This would strings to take the values of your JTexFields and pass them onto a textfile provided you have a FileWriter Class


Your action listener is not doing anything much right now.

Add code to add property in the following method:

public void actionPerformed (ActionEvent e)
{
    //add your code here
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜