0

public class CreateNewUser {

public String fullName;
public int age;
public String school;
public String username;
public String password;

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


public void setFullName(String n) {
    this.fullName = n;
}
public void setAge(int a) {
    this.age = a;
}
public void setUsername(String u) {
    this.username = u;
}   
public void setPassword(String p) {
    this.password = p;
}


public String getFullName() {
    return fullName;
}
public String getSchool() {
    return school;
}
public int getAge() {
    return age;
}
public String getUsername() {
    return username;
}
public String getPassword() {
    return password;
}


//write all info on to .txt 
public void writeToInfoFile(String u, String p, String s, int a) {

}



//write username & password to a sepereate file (users.txt)
public void writeToUsersFile(String u, String p) {
    try{
    PrintWriter output = new PrintWriter(file);

    //if username exists throw exception
    output.println(username + " " + password);
    output.close();

    } catch (IOException ex){
        System.out.println("ERROR: file not found.");
    }
}

}

public class CreateNewUserTest {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    CreateNewUser raul = new CreateNewUser();
    CreateNewUser jake = new CreateNewUser();


    String username, password;
    String uni;
    int age;

    //Raul
    System.out.printf("\tHello! Welcome to Ponder\t");
    System.out.println();
    System.out.print("Enter desired username: ");
    username = in.nextLine();
    raul.setUsername(username);

    System.out.println();
    System.out.print("Enter desired password: ");
    password = in.nextLine();
    raul.setPassword(password);

    raul.writeToUsersFile(username, password);


    in.close(); //close scanner


}

}

everytime I add a new username and passoword it replace the existing username and password. I want it to add the new username and password on a new line.

Raul Sanchez
  • 1,549
  • 3
  • 12
  • 7
  • I do not understand what is concretely the question – lrleon Oct 26 '15 at 01:32
  • 2
    Possible duplicate of [How to append text to an existing file in Java](http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java) – Yosef Weiner Oct 26 '15 at 01:32

2 Answers2

0

In the function writeToUsersFile, replace the following line:

PrintWriter output = new PrintWriter(file);

with

PrintWriter output new PrintWriter(new FileWriter("users.txt", true));

Also one more point, which are unrelated to your question:

Unless this is a toy application, I would recommend against writing username and password in plain text to files on disk. You don't want the passwords lying around like that on your disk. You may want to consider using some password stores (like Keychain on OS X) or Java Key Store from within Java.

Venkat
  • 462
  • 1
  • 5
  • 10
  • Thanks. And can you explain what exactly the boolean is for? Oh, and it's just a toy application. But also, what is a keychain and how can I use it with this application instead of a .txt – Raul Sanchez Oct 26 '15 at 07:25
  • 1
    Passing `true` for that second argument tells it to open the file for appending, instead of creating a new one. A key store or key chain is a secure storage for secrets. It is usually used for storing private keys. I was thinking they could store arbitrary passwords as well. But I'm not sure of it now. Instead, you could choose to encrypt your password to make sure it cannot be read in clear text by a malicious program. See http://blog.jerryorr.com/2012/05/secure-password-storage-lots-of-donts.html for more details. – Venkat Oct 26 '15 at 10:07
0

The file is being overwritten, using true appends to a file instead.Try this instead: PrintWriter output = new PrintWriter(new FileWriter(file, true));

Josh A
  • 51
  • 6