How to read the property file when duplicate key-value pair exist in the file?

20,816

Solution 1

PropertiesConfiguration from Apache Commons Configuration supports loading a properties file with multiple entries with the same key.

Use the getStringArray(key) method or getList(key) method to access all values for the specified key.

Solution 2

Properties file sets key value pairs. All keys are unique so it will not catch duplicate pairs. Instead it will fetch the latest mapped pair. For example :

Sample file:

a=1
b=2
c=3
d=4
a=10
c=7

The properties class will return latest pairs i.e

a=10
b=2
c=7
d=4

Still if your requirement is to find all the pairs whether duplicate or not , Use the following code that uses Scanner class and two arraylist objects.

        ArrayList k = new ArrayList();
        ArrayList v = new ArrayList();
        Scanner scan = new Scanner(new File("E:\\abc.properties"));
        while(scan.hasNextLine()) {
            //System.out.println(scan.nextLine());
            String split[] = scan.nextLine().trim().split("=");
            if(split.length == 2) {
            k.add(split[0]);
            v.add(split[1]);
            System.out.println("pair " + split[0] + ":" + split[1]);
            }
            //System.out.println("a");*/
        }

Solution 3

you can use buffer reader to read lines of your file and split the result:

public static void main(String[] args) throws IOException {
    Reader file = new FileReader("C:/file.cfg");
    BufferedReader br = new BufferedReader(file);

    String line = br.readLine();
    while (line != null) {
        String obj[] = line.split("=");

        for (int i=0 ;  i<obj.length; i=+2  ){
            System.out.println(obj[i]+"="+obj[i+1]);

        line = br.readLine();
        }
    }
}
Share:
20,816
user3089783
Author by

user3089783

Updated on July 05, 2022

Comments

  • user3089783
    user3089783 almost 2 years

    I am loading my property file using the load() of properties class.I am able to read the key-value pair of the property class using set,hashmap,treeset,enumeration, but it does not retrieve the duplicate pairs. The duplicate pairs are retrieved only once.

    • user207421
      user207421 over 10 years
      Your property file is invalid. Keys must be unique. Redesign it.
    • user3089783
      user3089783 over 10 years
      @EJP: i know its invalid if duplicate values exists in the property file and that is what i want to show by comparing each key with every other key.i want to create a method that If duplicate keys are present then it must show some alert or message,is there any way to do it?
    • user207421
      user207421 over 10 years
      Nothing stopping you writing such a method yourself, but that's not what you asked.
    • user3089783
      user3089783 over 10 years
      @EJP:pardon for the mistake,but for that method first i need to read all the keys from the property file even if its duplicate and i am not able to read the duplicate keys,please let me know the way to do that.... as in my property file i have a=1,a=1,a=2......i want to read all the three a but i am able to read it only once i.e. a=2....
    • user207421
      user207421 over 10 years
      You have to write it yourself. You can't use Properties.load().
  • user3089783
    user3089783 over 10 years
    Actually my property file has values: a=1,a=2,a=4.......... and i want to get all the three keys as i want to compare each key with every other in the file to show that duplicate keys exists in the file,is it possible anyhow?
  • Nishant Lakhara
    Nishant Lakhara over 10 years
    Then it will give a=1 as key value pair. Properties file should not contain duplicates ideally. Do you sill want to do it?
  • user3089783
    user3089783 over 10 years
    No, i want to print the keys that exists more than once in the property file
  • user3089783
    user3089783 over 10 years
    the code you provided above is quite helpful,its reading all the values even if its duplicate through the scan.nextLine() but throws no line found exception when storing in the Split[].
  • Nishant Lakhara
    Nishant Lakhara over 10 years
    There must be some problem with your file. Terminate the last line correctly. Dont press an enter after the last line or send me the file, I check with it.
  • user323094
    user323094 over 9 years
  • Emrys Myrooin
    Emrys Myrooin almost 9 years
    He is not asking for a piece of code without any explanation.
  • Starwarswii
    Starwarswii almost 7 years
    Note that this basic way of reading a property file will not properly deal with #comments and spaces before and after the equal sign, which are normally ignored.