如何使用 java 写入文本文件



我一直在使用"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");
            }
        }
    }
}

}

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

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

这将字符串获取 JTexFields 的值并将它们传递到文本文件中,前提是您有一个 FileWriter 类

您的操作侦听器现在没有做太多事情。

使用以下方法添加代码以添加属性:

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

相关内容

  • 没有找到相关文章

最新更新