Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 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 Reply

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...