3

I have a locally stored ini file which I am trying to parse as follows:

Ini ini = new Ini(new File("path/to/file")); 
System.out.println(ini.get("header", "key"));

But I keep getting a parse error exception message pointing to the line after the comment line of the ini file (#). This is what my ini file looks like:

File.ini

#Tue Oct 11 18:45:03 CST 2016
PVIDNO=PALMUS-00001
PVIDNo=SSI-1
Authentication=VALID
ModelNo=KD03816-B001
PALMUS-ID=73364
PV-ID=PALMUS-01
fabian
  • 80,457
  • 12
  • 86
  • 114
Kylie Irwin
  • 195
  • 3
  • 11

3 Answers3

2

You are using some class Ini that comes from who-knows-where; and that Ini-File parser simply doesn't like .ini files that contain "# comment" entries.

So, your options are basically:

  1. I forgot about that first, but maybe the "best" option: don't use "ini" files; but change to "property" files; which are a much more "natural" choice for Java applications. "Built-in" support for them; and hey, "# comments" work out of the box.
  2. If Ini is "your own code"; then you make your own code accept such comments
  3. If Ini is coming from some library, then you check if that library allows to influence the parsing process to allow for such comments.

If the library doesn't allow for such special handling, you have another two options:

  1. Look out for some other 3rd party library to parse your files
  2. "Talk" to the people providing the library you are currently using and convince them to somehow make their library work for you.
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I've checked the library and it does allow # comments. Will try what you said about changing it to property though. Thanks! – Kylie Irwin Oct 11 '16 at 11:17
2

You can do the same with Properties:

 Properties p = new Properties();
 p.load(new FileInputStream("user.props"));
 System.out.println("user = " + p.getProperty("DBuser"));
 System.out.println("password = " + p.getProperty("DBpassword"));
 System.out.println("location = " + p.getProperty("DBlocation"));

where the .ini file is:

# this a comment
! this a comment too
DBuser=anonymous
DBpassword=&8djsx
DBlocation=bigone
granmirupa
  • 2,780
  • 16
  • 27
1

Have you tried using Properties?

Creating Config:

Properties prop = new Properties(); OutputStream output = null;

try {
        SaveSucessful = true;
    output = new FileOutputStream("config.jar");

    // set the properties value
    prop.setProperty("PVIDNO", "PALMUS-00001");

    // save properties to project root folder
    prop.store(output, null);

} catch (IOException io) {
    io.printStackTrace();
} finally {
    if (output != null) {
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    }

Reading Config:

Properties prop = new Properties();
    InputStream input = null;

    try {
            LoadSucessful = true;
        input = new FileInputStream("config.jar");

        // load a properties file
        prop.load(input);

        // get the property value and print it out
        PlayerName = prop.getProperty("PVIDNO");

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

This should work perfectly.

Mine Rockers
  • 213
  • 3
  • 12