I have an application that stores users data in a text file and I need to store only unique entries (e.g. a phone book), where out of the following fields: name, mobile number, and address, mobile number would be the unique key.
I'm using the following code to write my file:
try {
line = mUserName.getText().toString() + ","
phone.getText().toString() + ","
address.getText().toString();
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
String FILENAME = path + "/UsersData.txt"; // External is the text file name
FileWriter fWriter;
BufferedWriter writer;
fWriter = new FileWriter(FILENAME, true);
writer = new BufferedWriter(fWriter);
writer.append(line);
writer.newLine();
writer.close();
} catch (Exception e) {
result.append("\n" + e.getMessage());
}
What I'd like is for the application to check whether the phone number already exists in the file before it writes the new data, and if it does we tell the user that it's already in the file.
Is it possible to do this without harming application performance?
There's another option that we could store the data in a database, then convert it to a text file when they ask for it, would that be better?