Reputation: 892
I am currently doing a programming assignment and it says that we should "store all the data in a data file and need to check the compatibility of the user PIN and account no. validity"
Unfortunately my lecturer has not taught us about data files, and when I googled I found two different answers,
MY QUESTION IS WHICH ONE IS A DATA FILE? and how do you retrieve the user PIN (after its typed from buffer reader) to check whether both are correct? Any Help is GREATLY APPRECIATED!!
Upvotes: 0
Views: 24483
Reputation: 114757
The easiest way is using the Properties
class. It stores key/value pairs and can persist the data to a properties files. Here's a working example:
Properties p = new Properties();
p.setProperty("johndoe.pin", "12345");
p.store(new FileWriter("myfile.properties", "");
and reading:
Properties p = new Properties();
p.load(new FileReader("myfile.properties"), "");
The check will be done with the properties object:
public boolean isValid(String user, String pin) {
return pin.equals(p.getProperty(user + ".pin"));
}
It is easy but of course there's no encryption. The file is stored in plain text including the PINs.
Upvotes: 4
Reputation: 22751
Serialization is another method of persistent storage.
You can create a class that implements Serializable with a field for each piece of data you want to store. Then you can write the entire class out to a file, and you can read it back in later. I believe the Serializable interface outputs the class into some sort of binary/hex representation.
There are other ways to to do serialization without using the standard Java serialization, like you can use a JSON library.
Upvotes: 0
Reputation: 1628
It doesn't matter whether it's txt or csv. You can even save it in xml. Don't get confused with file extension. The only thing you should care about is "how you can save/load those data".
Good luck
Upvotes: 0
Reputation: 3557
from wikipedia:
A data file is a computer file which stores data for use by a computer application or system.
So both a *.txt
file and a *.csv
file are data files.
A *.csv
(comma-seperated values) file is in essence a *.txt
file that separates your values by a ,
The 2 methods you found should do about the same, but it will be easier to use the csv-method.
Upvotes: 2