How to read a properties file when a duplicate key-value pair exists in a file?

I upload my properties file using the property class load(). I can read the key-value pair of the property class using set, hashmap, treeet, enumeration, but it does not extract duplicate pairs. Duplicate pairs are retrieved only once.

+4
source share
3 answers

The properties file defines key value pairs. All keys are unique, so they will not catch duplicate pairs. Instead, he will get the last matching pair. For example:

Example file:

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

The property class will return the last pairs ie

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

, , , , , arraylist.

        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");*/
        }
+5

PropertiesConfiguration Apache Commons Configuration .

getStringArray () getList () .

+5

you can use the buffer to read the 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();
        }
    }
}
-1
source

All Articles