Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

java - Ini4j - How to close the file stream after parsing error

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?

question from:https://stackoverflow.com/questions/65848101/ini4j-how-to-close-the-file-stream-after-parsing-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...