Matth963
Matth963

Reputation: 29

create a text file in java

I have a problem about creating a textfile with the name I want.

I want to create a textfile named : 'username' Subjects.

 private void saveSubjects(){

    RegisterFrame r = new RegisterFrame();


    String username = r.txtUser.getText();;

    try{
        FileWriter f = new FileWriter(username + "" + "Subjects" + ".txt", true);

        String subjects[] = lstSubjects.getItems();

        for(int i = 0; i<subjects.length; i++){
            f.write(subjects[i] + "\r\n");
        }
        f.close();
        JOptionPane.showMessageDialog(null, "Data saved!", "Data Saved", JOptionPane.INFORMATION_MESSAGE);
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, "Nothing Inputted!", "Error", JOptionPane.ERROR_MESSAGE);
    }

}

I want to get the username from RegisterFrame as it is inputted there but it's not working. I know it's a simple thing but I'm still a beginner in this. How can I solve this?

Thanks in advance

Upvotes: 1

Views: 2874

Answers (3)

kajacx
kajacx

Reputation: 12939

try this:

String username = r.txtUser.getText();
System.out.println("The loaded username is: " + username);

then you will see where your problem is : writing into the file OR getting the username text.

If the problem is in getting the text, consider other way of getting it or modify the question by removing the file write part and specifiing the username getting part.

Otherwise, IDK where the error is.

BTW: how is it not working? the file is not created at all? do you see any errors? the file has wrong name? please specify

Upvotes: 2

dimo414
dimo414

Reputation: 48794

Assuming new RegisterFrame() starts up a GUI window, the issue is your code runs before you have a chance to type in your name. Instead you need to use event listeners to capture the contents of text fields, otherwise the code to get the name runs immediately after the window opens, long before you have a chance to type anything in.

The timeline is like this:

  1. RegisterFrame starts a new thread to display the GUI without blocking your code
  2. Your code immediately pulls "" from txtUser, which is of course empty
  3. Now you type your name in
  4. Nothing happens, because nothing in your code is paying attention to that action

Instead, it should be:

  1. RegisterFrame starts a new thread to display the GUI without blocking your code
  2. The method returns, or starts doing work that isn't dependent on the GUI
  3. Now you type your name in
  4. An event listener is triggered from the new thread, and the associated action to get the name and write to a file is executed

You have to decide what sort of listener makes sense for your use case, for instance you might want to wait until the user clicks a button (that says "Submit" or "Write File" for instance) and register an ActionListener on that button. Then you put your username polling and file writing behavior in that action* and you're golden!

*I should add that in truth you want to do as little as possible in ActionListeners, and it would be better to check if the username is not empty, then pass the actual work off to another thread, for instance with a SwingWorker, but for your purposes I suspect it will be alright to not worry about that.

Upvotes: 0

Edd
Edd

Reputation: 8570

Your code for writing the file seems to be fine. Based on your code I tried this which worked perfectly:

public static void main(String[] args) {

    FileWriter f = null;
    try {
        f = new FileWriter("Subjects.txt", true);

        String subjects[] = {"subject1", "subject2"};

        for (String subject : subjects) {
            f.write(subject + "\r\n");
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(f);
    }
}

I'd say your problem is elsewhere.

Please note that best practice dictates that Closeable objects such as FileWriter should be closed in a finally block

Upvotes: 0

Related Questions