3

So I'm trying to store a list of users into an external text file, and If they dont have an account already, then they need to sign up, and their account gets added to the text file.

However each time I create a new user, it overwrites the last one in the text file.

Can anyone see what wrong with my code for creating a new user, and if theres something obvious that would fix it?

EDIT:

I believe that the text file is being recreated every time that I run the program, how can I just add to it, instead of making a new one each time?

System.out.println("Enter your full name below (e.g. John M. Smith): ");
        String name = scanner.nextLine();
        System.out.println("Create a username: ");
        String userName = scanner.nextLine();
        System.out.println("Enter your starting deposit amount: ");
        double balance = scanner.nextInt();

        System.out.print(dash);
        System.out.print("Generating your information...\n");
        System.out.print(dash);

        int pin = bank.PIN();
        String accountNum = bank.accountNum();

        User user = new User(name, userName, pin, accountNum, balance);

        // new user gets added to the array list
        Bank.users.add(user);

        System.out.println(user);

    }

    try {

        File file = new File("users.text");

        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter  bw = new BufferedWriter(fw);

        bw.append(String.valueOf(Bank.users));
        bw.close(); 

        System.out.print("DONE");

    } catch (IOException e) {
        e.printStackTrace();
    }

4 Answers 4

3

Your problem is that when you create your FileWriter instance you are not asking it to append. It defaults to overwriting.

Try this (extra lines for context):

    if (!file.exists()) {
        file.createNewFile();
    }

    // Line being changed
    FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);


    BufferedWriter  bw = new BufferedWriter(fw);

    bw.append(String.valueOf(Bank.users));
    bw.close(); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! You've been a huge help to me!
1

Something like this should work:

String filename= "users.test";
FileWriter fw = new FileWriter(filename,true); //the true will append the new data
fw.write(String.valueOf(Bank.users));//appends the string to the file
fw.close();

Give this a try. Do add exceptions and stuff to it off course.

Comments

1

That's beacause the file gets truncated on open. You have to open it in append mode, with:

FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);

The second parameter will tell the FileWriter to do so. Reference here.

Comments

1

Replace this:

FileWriter fw = new FileWriter(file.getAbsoluteFile()); //overwrite

by this:

FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); //append

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.