Ini4j - How to close the file stream after parsing error

167 views Asked by At

I have the following code below which uses the ini4j library to load a file as a .ini file.

Ini configIni;

public void setupConfig()
{
    String configFilePath = "config.ini";
    
    File config = new File(configFilePath);
    
    if(config.exists())
    {
        try
        {
            configIni = new Ini(config);
        }
        catch(Exception e)
        {
            config.delete();
            config.deleteOnExit();
            e.printStackTrace();
        }
    }
}

In the event that it fails to parse correctly it will throw an exception. Problem is, I want to get rid of the config file when this happens in order to replace with it a default one. But for some reason the application holds onto the file even though configIni is null.

I don't know how to tell ini4j or Java to let go/close the stream of the config file so I can delete it. Any ideas how I can do this?

1

There are 1 answers

0
queeg On BEST ANSWER

In the current code it seems the Ini class works on the file and does not give you any access to the stream.

So either

  • you rely on that lib to already close the stream
  • or you might try to find an alternative constructor that reads the ini from an inputstream. In that case stream handling is in your control and you should be able to close it in a finally block.