CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: Reading .ini files

  1. #1
    Join Date
    Apr 2019

    Reading .ini files

    I'm in a process of translating some old code and I'm stuck on reading some specific ".ini" files, I use
    like this:

    Properties prop = new Properties();
    prop.load(new FileInputStream("sample.ini"));
    prop.forEach((key, value) -> System.out.println("Key : " + key + ", Value : " + value));
    This is the "sample.ini":

    But the section's end up being read as keys, this is what I get as an output:
    Key : Key1.1, Value : Val1.1
    Key : Key1.2, Value : Val1.2
    Key : [Sec2], Value :
    Key : Key2.1, Value : Val2.1
    Key : [Sec1], Value :

    If needed, I could work with this and handle the section names myself, but then the problem is that the data is not stored in the same order as it was in the input file.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004

    Re: Reading .ini files

    Search "how to read an ini file in java" in bing or google. There are a lot of examples.

  3. #3
    Join Date
    Apr 2019

    Re: Reading .ini files

    I found this solution on SO and it worked great.

    public static Map<String, Properties> parseINI(Reader reader) throws IOException {
    Map<String, Properties> result = new HashMap();
    new Properties() {

    private Properties section;

    public Object put(Object key, Object value) {
    String header = (((String) key) + " " + value).trim();
    if (header.startsWith("[") && header.endsWith("]"))
    return result.put(header.substring(1, header.length() - 1),
    section = new Properties());
    return section.put(key, value);

    return result;

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)