1

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?

  • can u retrieve the data from the text file line by line? – cw fei Sep 23 '15 at 08:20
  • Java has a Set that keeps unique values. You can read your file rows to Set, add your value, and set all rows from set to file – Gaskoin Sep 23 '15 at 08:20
  • I'm a beginner in java and android so I appreciate it if there was some example or a link to read. thanks friends –  Sep 23 '15 at 08:24

1 Answers1

0

If you write 2 or more thing with single line without seperate them with a character(i.e: #,% etc) will not be logical. If username ends with a number, you will not determine that this number is character of username or phone number. So "line = mUserName.getText().toString() + phone.getText().toString() + address.getText().toString();" is wrong.

You should create a object class which has fields as userName, phoneNumber and address as:

    public class Contact {

    private String userName;
    private String phoneNumber; //should be string it may start with +1 123...
    private String address;

    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

Then set your contact informations as a list, save and retrieve your contact list via using GSon and shared preferences via this answer: store and retrieve a class object in shared preference

You can search user contact information via retrieving your list from shared preferences.

Community
  • 1
  • 1
Sedat Polat
  • 1,631
  • 2
  • 18
  • 28
  • I'm using a separator but I forgot to add it when writing the question, I edited my question. And for your answer it will be very complicated for me as a newbie to java and android and how will it effect performance if there were thousands of contacts? and what about using databases to store contacts and set the phone number as a unique field? –  Sep 25 '15 at 13:14
  • 1
    Using DataBase is the better idea, if there will be thousands of data. Shared Preferences is better to store less data, sth like user setting, user profile data... – Sedat Polat Sep 25 '15 at 18:56