如何使用java写入文本文件?

时间:2011-05-21 12:45:23

标签: java file text-files

我一直在使用“Learning Java 2nd Edtion”一书来尝试让我的java应用程序将我的输入写入名为properties的文本文件。我已经将教科书示例操作到我自己的代码中,但仍然在尝试使其工作时遇到问题。我想我可能需要把它连接到我的提交按钮,但这章内没有提到。

基本上我试图将信息存储在文本文件中,然后在另一个位置使用该文本文件来读取所有属性详细信息。

这是我的AddProperty页面的代码到目前为止,我们将非常感谢任何建议。 目前iv撞墙。

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

}

2 个答案:

答案 0 :(得分:2)

在您执行的操作中,您没有说明任何内容,例如您执行的操作可以添加。

   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);

            }
       });

如果你有一个FileWriter类,这将获取JTexFields的值并将它们传递给文本文件的字符串

答案 1 :(得分:1)

你的动作听众现在没有做任何事情。

添加代码以通过以下方法添加属性:

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