0

I am trying to create a very simple account system where a user can create an account and "login" to an account that they have created. At the moment after the user inputs their details I add the account to a text file like so;

public void createAccount(UserInfo createdUser) throws FileNotFoundException, IOException{
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("auctionAccounts.txt"));
    objectOutputStream.writeObject(createdUser);
    objectOutputStream.flush();
    objectOutputStream.close();
}

I have now moved onto the "login" method, and I am struggling to find a way to loop through the objects stored in the textfile to see if an account exists. I just want to do a very simple compare such as;

if(enteredUsername == textfile.username){
 if(enteredPassword == textfile.password){
    //Do something
 }
}

Obviously that is the simple pseudo code, I am teaching myself Java and haven't come across a way to loop through a text file at the moment. I understand that I could probably use an ArrayList of accounts, but I would like it if when the user comes back to my program after exiting their account would still exist.

Many thanks

Michael_GG
  • 85
  • 5
  • How are you storing the objects in the file? Could you post an example of the text file? – Luke SpringWalker Nov 28 '14 at 12:24
  • Your `createAccount` method always opens the file from the start, meaning that there will always be just one account in the file - the latest one. I suggest going through a tutorial about [creating, reading and writing files](https://docs.oracle.com/javase/tutorial/essential/io/file.html). – RealSkeptic Nov 28 '14 at 12:30
  • Actually at the moment the formatting of the stored account is very messed up, here is an examples of a username, "TestAccount", and a password, "TestPassword"; `¬í sr UserInfoó0‡´ù±´ L passWordt Ljava/lang/String;L userNameq ~ xpt TestPasswordt TestUsername` – Michael_GG Nov 28 '14 at 12:30
  • 1
    Yes, because you chose to use ObjectOutputStream, which uses serialization, rather than a simple text file. I repeat my suggestion. – RealSkeptic Nov 28 '14 at 12:32
  • Ok, thanks for your suggestion, I will read through the tutorial you suggested. – Michael_GG Nov 28 '14 at 12:37

1 Answers1

2

You have to read your UserInfo from your file using FileInputStream and ObjectFileInputStream. Then you have to iterate over the objects you read and compare the username and the password. Something like this should work:

ObjectInputStream input = new ObjectInputStream(new FileInputStream("auctionAccounts.txt"));
UserInfo user = null;

while (user = (UserInfo) input.readObject() != null) {
  if(user.username.equals(enteredPassword) && user.password.equals(enteredPassword)) {
    // do something
  }
}

You have to compare String instances with equal. See: Java String.equals versus ==

As @RealSkeptic explained you don't append to the file. You recreate it every time add a UserInfo.

Last but not least: Don't use such naive code with real users. At least you should created salted hashes of the passwords.

Community
  • 1
  • 1
gregor
  • 4,733
  • 3
  • 28
  • 43