-2

Hello fellow stack overflow users.

I have a problem that I have no idea how to face... I want to incorporate a login screen into one of my Netbeans projects that requires a user to either create a user name and password or login using a existing password and username. When the user logs into their personal account it must display which their personal high-scores. All the information has to be read and written to a already created text file in the project file.

My login screen

Any suggestions to help improve the concept or where I can read up more on Text file databases would also be much appreciated.

Thanks from:

Noobie Coder

EDIT: This is part of an school I.T assignment, we had a choice to make a functioning login system using either a textfile or a MySQL database. It does not need to be a secure login system with layers of encryption or extra security measures.

What I am struggling with:

Honestly I just don't know how to begin. I don't have the faintest clue of I/O and have no clue how to incorporate it into my project.

  • Is this a school project? Because it's a bit ridiculous to want to store user credentials in a plain text file in a real-world project. If that's the way you need to do it though, and security isn't a concern, then you need to tell us what part you're having problems with? You want to know what format to use? Well, you could just use CSV with one line per account, or you could serialize a full Accounts object, or you could store XML, or do it a hundred ways. We need more info on what you've tried and where you're having trouble. – Jim Oct 14 '15 at 15:47
  • This question is too broad. There's a whole bunch of methods of storing data to a text file (melli-182 has already listed a few of them) and just as many methods to read them. Pick one, try to get it to work, then if you have and *specific* questions, come back and we'll be glad to help. – Mage Xy Oct 14 '15 at 15:50
  • Hey I edited my post. I just need a simple way to read data from a text file that only users with the right password can access. That will displayed in a textfield. – The Noobie Coder Oct 14 '15 at 15:54
  • If your question is simply about how to read/write text files, then this question is a duplicate. Reading/writing to files is something that has plenty of results not just on SO, but on many other programming sites as well. See [this question](http://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file-in-java) for reading, and [this question](http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a-file-in-java) for writing. Both are the top results from Google when searching "stackoverflow java read/write file" – Mage Xy Oct 14 '15 at 15:59
  • One of my main concerns is not only to read and write but to make sure that only if CorrectUserName = CorrectPassword it will go to that users data. Is that covered in those two links? – The Noobie Coder Oct 14 '15 at 16:03
  • Yes and no. Programming is all about learning how to put pieces of information together. The biggest hurdle you have right now is getting the information from the file. Once you've got the info, you just have to store it accordingly (maybe in an ArrayList or more likely a Map). Then user validation is as simple as looking up the username and validating it. – Mage Xy Oct 14 '15 at 16:45
  • Either way, you need to try something first, and if you have any specific question, we can help. If you don't give us a starting point of what you've tried, you may get 20 different answers with 20 different approaches, and that's just not how SO is meant to work. – Mage Xy Oct 14 '15 at 16:47
  • Nik G , I get what your saying it's just i feel a bit overwhelmed. I am basically self taught because our programming teacher is useless. So all of this is new concepts to me. – The Noobie Coder Oct 14 '15 at 17:17

1 Answers1

1

A way to encrypt your text file would be to use a Cipher.

https://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html

There are also tutorials for reading/writing an encrypted file with Java:

https://www.flexiprovider.de/examples/ExampleCrypt.html

Edit to match your edited question again:

You can set the encryption key to the password a user has to enter. You could create a .txt, encrypted with the password of the user, containing the users scores. To read the encrypted file line by line, use a BufferedReader (in the example they write the input to another file). To use the code from the example:

String cleartextAgainFile = "cleartextAgainSymm.txt";
cipher.init(Cipher.DECRYPT_MODE, secKey);
fis = new FileInputStream(ciphertextFile);
CipherInputStream cis = new CipherInputStream(fis, cipher);
BufferedReader read = new BufferedReader(new InputStreamReader(cis));
String str;
while((str=read.readLine())!=null){
   //do sth with the read line
}
read.close();
Friwi
  • 482
  • 3
  • 13
  • Thanks a lot, I won't need encryption at this moment in time but I will definitely look into it for future larger projects. – The Noobie Coder Oct 14 '15 at 15:56
  • The new InputStreamReader(InputStream) constructor can contain any type of InputStream, so you can read a file with it aswell. To write a file line by line, you can use a PrintWriter (works similar to System.out then) – Friwi Oct 14 '15 at 16:03