Best way to store data between program runs in java?

pie154 picture pie154 · Aug 16, 2009 · Viewed 13.1k times · Source

What is the best way to store data between program runs in Java? I already know that you can use a text file and store the information that way, but I was wondering if there is a better way to store the information that is generated by the program between runs of the program.

Also, is there any way to do it so as to keep the information secure? Specifically, I want to keep the end user from being able to access it.

Answer

Michael Borgwardt picture Michael Borgwardt · Aug 16, 2009

I was wondering if there was any way other placing the information that is genereated by the program between runs of the program?

Simply use an ObjectOutputStream to serialize it into a file and an ObjectInputStream to get it back.

Also is there any way to do it so as to keep the information secure? from the end user being able to access it?

If the code runs on the end user's system then no, there is no way to prevent them from getting the data - it's not even worth your time trying to encode it somehow, since it's easy to attach a debugger and inspect the program's state while it's running. As a binary format, Java serialization will prevent non-technical users from deciphering it, and that's pretty much the best you can hope for.