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

Thread: Reading .ini files

  1. #1
    Join Date
    Apr 2019
    Posts
    4

    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
    Code:
    java.util.Properties
    like this:

    Code:
    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":
    [Sec1]
    Key1.1=Val1.1
    Key1.2=Val1.2
    [Sec2]
    Key2.1=Val2.1


    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 / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,063

    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
    Posts
    4

    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;

    @Override
    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());
    else
    return section.put(key, value);
    }

    }.load(reader);
    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)