Reading files with code value pairs I have a text file with about a hundred code value pairs [$code, Value].

For example:

Sample file

$codeA
valueOfA
$codeB
valueofB
.
.
.


These values need to be initiated into textFields, radioButtons, checkBoxes, etc over "several panels" contained within a main Controller panel. I switch between panels through a simple card layout.

I read through the lines and use if/else statements to get values for the fields, which isn't efficient. I was wondering if there is a better approach, a more elegant approach maybe.

    /*
 * read File
 */
private void readFile() {
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(fileName));
        String line = null;
        while ((line = br.readLine()) != null) {
            if (line.equals("$codeA")) {
                panel1.txtFieldA.setText(br.readLine()));
            } else if (line.equals("$codeB")) {
                panel2.txtFieldB.setText(br.readLine()));
            }
            //.....
            //.....
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Make a Map like this

Map&lt;String, JTextField&gt; map = new HashMap&lt;&gt;();
map.put("$codeA", panel1.txtFieldA);
map.put("$codeB", panel2.txtFieldB);


and change your loop like this

String code = null;
while ((code = br.readLine()) != null) {
    JTextField jtf = map.get(code);
    if (jtf != null)
        jtf.setText(br.readLine());
    else
        br.readLine();
}