0

I've been working on a server for my hobby game and I'm finally ready to implement loading/saving, I was planning on doing a tutorial series for the server/client (creating an multiplayer 2d game) so I wanted to do the player loader/saving through text files, so I didn't have to have SQL and JDBC tutorials on the side.

When creating the account, this is what is saved to the text file

writer.println("// Account Name");
writer.println("[Username] = " + username);
writer.println("[Password] = " + password);
writer.println("// Player Data");
writer.println("[PlayerX] = 0.0");
writer.println("[PlayerY] = 0.0");

However, I wanted to skip the line that stored the password while storing, so I can dispose of the password value at login.

So, in my save code, as you can see I have this

Misc.log("Saving Character File for user: " + username);
writer = new PrintWriter(accountDir + username+".txt", "UTF-8");
writer.println("// Account Name");
writer.println("[Username] = " + username);

writer.println("// Player Data");
writer.println("[PlayerX] = " + p.getX());
writer.println("[PlayerY] = " + p.getY());
writer.close();

Now, Obviously, Deleting that line of code isn't going to do anything for me, but I can't really figure out how I should go about "skipping" that line, I thought about using a BufferedReader to Read each line before writing, then returning if the line started with "[Password]" however, that would be really inneficient, I would think.

What would you guys recommend for this? Could you write it out in Psuedo (or just regular...?) code for me so I can finish this off.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Christian Tucker
  • 627
  • 8
  • 20
  • But. but. but you wouldn't ever store a plaintext password, would you? Use a hashcode! – tilpner Dec 16 '13 at 15:50
  • FYI, saving clear text passwords is a BAD IDEA™. I realize it's a hobby thing so you may not care, but in case you do, http://stackoverflow.com/questions/7017688/what-is-the-best-practice-for-securely-storing-passwords-in-java – Taylor Dec 16 '13 at 15:51
  • You problem is still a bit unclear to me. You want to save the password and then not save the password? Not sure what you mean by `skipping a line`, but if you don't save the password, how does the user login next time? – Taylor Dec 16 '13 at 15:52
  • I'm very aware that storing this raw is a horrible idea, however it's just for learning purposes at the moment, I will worry about security at a later time. What I mean by "Skipping" is I don't want to write on that line. So, I'll write on line [1,2,3,5,6] (Skipping line 4) – Christian Tucker Dec 16 '13 at 15:56
  • If I understood it right, the problem was reading from the user data file after skipping the password since the data was raw and needed to be parsed properly to get each data. – dARKpRINCE Dec 16 '13 at 15:56
  • Not exactly, I'm just asking how to not write to that line. I don't want to over-write it. – Christian Tucker Dec 16 '13 at 16:03
  • For the time being, I'll just store the players password in the Player Entity and write over the password with each save. – Christian Tucker Dec 16 '13 at 16:06

1 Answers1

0

My suggestion would be to use Properties in Java and create properties files to access and save data easily.

For eg.:

Properties userData;
userData.load(in) // in is inputsteam or Reader of the user file
// Save the username
userData.setProperty("username", "dARKpRINCE");
// Get the username
userData.getProperty("username")
// Save the property file
userData.store(out, "Saved on Exit"); // out is outputstream or writer of the user file

This way you won't have issues by skipping any data to be saved or adding new ones.

dARKpRINCE
  • 1,538
  • 2
  • 13
  • 22