Config File Parser in Java: Explanation & Practice

Parse a key=value configuration file

Problem summary

Read a config file with key=value pairs and provide get(key) functionality.

Starter code

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        // Read config.properties
        // Parse key=value pairs
        // Print value for given key
    }
}

Expected output and test cases

  • Get host
    database.host = localhost
  • Get port
    database.port = 5432
  • Missing key
    Key not found

Hints

  1. Use Properties class or custom parsing
  2. Split each line by =
  3. Store in HashMap

Related File I/O exercises

Practice all File I/O exercises · Run this idea in the Java compiler