简体   繁体   中英

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)".

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:

public String getText()

JTextField is just the GUI element, getText() doesn't change it. You should store the result in a String variable and then use it to write() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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