Home > Mobile >  How do I add text from a string to a pre-existing txt file?
How do I add text from a string to a pre-existing txt file?

Time:08-08

I am writing a Java program that is a password authenticator. The user enters a password and then is asked to confirm the password by re-entering it. If the second password matches the first the password will be confirmed and set to the password variable. But if the passwords don't match then the second password entered should be recorded and logged in a pre-existing txt file. As well as this the loop will start again and the user will be asked to re-enter their passwords and confirm them.

I have tried creating a file and then writing to it in the else if condition, however then that means the each time the user enters a wrong password the txt file is created again and only the last entered password is recorded instead of the entire list of wrong passwords that were entered.

Here is my code below.

package passwordAuthenticator.java;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.Formatter;

public class passwordAuthenticator {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        String password;
        
        try {
              File myObj = new File("Passwords.txt");
              if (myObj.createNewFile()) {
                System.out.println("File created: "   myObj.getName());
              } else {
                System.out.println("File already exists.");
              }
            } catch (IOException e) {
              System.out.println("An error occurred.");
              e.printStackTrace();
            }
        

        while(true){
            System.out.println("Please enter password: ");
            Scanner sc = new Scanner(System.in);
            String firstPassword = sc.nextLine();
            
            System.out.println("Please confirm password: ");
            Scanner sc1 = new Scanner(System.in);
            String secondPassword = sc.nextLine();
            
                if(firstPassword.equals(secondPassword)) {
                    System.out.println("Password confirmed.");
                    password = secondPassword;
                    break;
                } else if(firstPassword != secondPassword) {
                    try {
                          FileWriter myWriter = new FileWriter("Passwords.txt");
                          myWriter.write(secondPassword);
                          myWriter.close();
                        } catch (IOException e) {
                          System.out.println("An error occurred.");
                          e.printStackTrace();
                        }
                    System.out.println("\nPasswords are not a match. Retry!\n");
                }
                    
            }
            
        }
    }

Thank you for your time looking at this with me.

CodePudding user response:

Your programs works well. The only change is this.
You should use this constructor. Just add true as the last parameter

FileWriter myWriter = new FileWriter("Passwords.txt", true);

Here's the description

/**
 * Constructs a FileWriter object given a file name with a boolean
 * indicating whether or not to append the data written.
 *
 * @param fileName  String The system-dependent filename.
 * @param append    boolean if <code>true</code>, then data will be written
 *                  to the end of the file rather than the beginning.
 * @throws IOException  if the named file exists but is a directory rather
 *                  than a regular file, does not exist but cannot be
 *                  created, or cannot be opened for any other reason
 */
public FileWriter(String fileName, boolean append) throws IOException {
    super(new FileOutputStream(fileName, append));
}

CodePudding user response:

public FileWriter(String fileName, boolean append) should work for you.

  • Related