-1

Okay, I was wondering if someone can explain, in great detail, how to make my JPasswordField save the contents that where entered into it, into a .txt file. So when I open the program again, I can have that person log in and it will get if thats the password or not. If it is the password, then the JOptionPane will close. I have this all in a JFrame, and the "Log In" button is in a JMenuBar in a JMenu, named "File". In the text file, I'd like to have the user enter a username and password, and it will set a username and a password to that user, like this. ex.

Username = Gavin

Password = 123

So if there are ways of just storing a certain variable, to that text file and make the program load that variables value, it'd be great if you can explain. I am also very open to spoonfeeding because I don't just take and use it, no. I'm going to look at it, figure out why it works because I'm going to work on duplicating it in a different way. Thanks in advance!

P.S. Yes, I did do some research, but the problem is, people would put a code that I have no clue on how it may start to work, I am farely new to Java programming. So, please do paste codes, I will study them, but if you can also throw in a minor explanation.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Gavin
  • 91
  • 1
  • 1
  • 2
  • 2
    [Basic IO tutorial](http://docs.oracle.com/javase/tutorial/essential/io/). Also, you might want to encrypt that password. – Jeffrey May 05 '12 at 16:48
  • 1
    Also, make sure you do not convert the password to a string (see http://stackoverflow.com/questions/10443308/why-gettext-in-jpasswordfield-was-deprecated) – Robin May 05 '12 at 16:51
  • 1
    *"people would put a code that I have no clue on how it may start to work"* (shudder) Your users should not be trusting you with their passwords at run-time, let alone when you go to serialize them. – Andrew Thompson May 05 '12 at 17:12
  • 2
    @Jeffrey He definitely should *hash* that password. – user207421 May 05 '12 at 22:19

1 Answers1

-2

I believe this is exactly what you are looking for - http://cs.saddleback.edu/rwatkins/CS4B/Crypto/FileEncryptor.html

The code is well documented but if you have any questions just ask them and I'll try to answer them

Well that is the simplest way (probably) to do it using encryption. Which is definitely a good idea. However the simple part of just writing and reading to a text file can be done by

39:       filename = "clear.txt";
40: 
41:       // Password must be at least 8 characters (bytes) long
42: 
43:       String password = "super_secret";
44: 
46:       outFile = new FileOutputStream(filename);
80:       outFile.write(password);

and this method to read which is more complicated but i will try to explain it

private static String readFileAsString(String filePath) throws java.io.IOException{
    byte[] buffer = new byte[(int) new File(filePath).length()];
    BufferedInputStream f = null;
    try {
        f = new BufferedInputStream(new FileInputStream(filePath));
        f.read(buffer);
    } finally {
        if (f != null) try { f.close(); } catch (IOException ignored) { }
    }
    return new String(buffer);
}

Basically byte[] is an array (google it if your not sure what that is) of raw 1s and 0s. It puts all the 1s and 0s of the file into and array (that is the same length as the file i.e. (int) new File(filePath).length()) Then a fileinputstream does all the wonderful magic to turn these 1s and 0s into text. The BufferedinputStream is just an efficient wrapper for the fileinputstream because it reads lots of bytes at one time then converts them all at the same time (it stores them in a buffer hence the name) versus reading and converting 1 byte at a time which is slow and inefficient (FileInputStream by itself). The exceptions youll want to google but basically it's just so your program doesn't crash if you cant find the file or you don't have permissions to read/write from it.

ghostbust555
  • 2,040
  • 16
  • 29
  • I looked at it and it looks extremely complex, I think this is wayy more advanced as to where I am at, at this point. I appreciate your answer, I will still study this code and I'm going to research these terms that I havn't even seen before, such as the bytes[], the main method throws exception? I've never seen that before. – Gavin May 05 '12 at 17:02
  • 1
    -1: password-based encryption has nothing to do with what the OP is asking, and your IO code is full of bugs. – JB Nizet May 05 '12 at 17:18
  • Yes, what JB Nizet said, I do not want a password encryptor, mainly because this is just for my practice, not releasing or anything... Can someone help me out on this one? – Gavin May 10 '12 at 04:18