简体   繁体   English

如何将Strings数据写入文本文件-Java

[英]How to write a Strings data to a text file - Java

I have this account creation program I'm working on, and would love to save the persons name, last name, email and password to a text file. 我正在使用此帐户创建程序,并且希望将人员姓名,姓氏,电子邮件和密码保存到文本文件中。 The following snippet should do just that, but the error message I'm getting when I put a String variable in the .write method is, "no suitable method found for write(JTextFeild)". 下面的代码片段应该这样做,但是当我在.write方法中放置一个String变量时,我得到的错误消息是“找不到适合write(JTextFeild)的方法”。

private void signupActionPerformed(java.awt.event.ActionEvent evt) {                                       
    fname.getText();
    lname.getText();
    email.getText();
    reemail.getText();
    password.getText();
    repassword.getText();

    if(male.equals(true)) {
        males = true;
    }
    if(female.equals(true)) {
        females = true;
    }

    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter( new FileWriter("UserPass.txt"));
        writer.write(fname);

    }
    catch ( IOException e) {
    }
    finally {
        try {
            if ( writer != null) {
                writer.close( );
            }
        }
        catch ( IOException e) {
        }
    }
}                             

Any ideas on how to fix this? 有想法该怎么解决这个吗?

From the documentation of getText() in javax.swing.text.JTextComponent: 从javax.swing.text.JTextComponent中的getText()文档中:

public String getText() 公共字符串 getText()

JTextField is just the GUI element, getText() doesn't change it. JTextField只是GUI元素, getText()不会更改它。 You should store the result in a String variable and then use it to write() . 您应该将结果存储在String变量中,然后将其用于write()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM