简体   繁体   中英

File not found java ini4j

I am trying to write something to a ini file using ini4j.

When i call the store() method it throws a FileNotFound exception even though it is in my project directory.

Maybe i did something wrong with my code?

Main:

public class Main {

    public static Wini ini = null;

    public static void main(String[] args) {

        Config conf = new Config();
        try {
            conf.setMultiOption(true);
            ini = new Wini();
            ini.setConfig(conf);
            ini.load(new File("apikeys.ini"));
        } catch (InvalidFileFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

The file where i attempt to write and store the data:

if (KeyEndpoint.isValid(apikey)) {

                    Main.ini.put(apikey, Main.ini.get("Apikey"));

                    try {
                        Main.ini.store();
                    } catch (IOException e) {
                        channel.sendMessage("Invalid api key.").queue();
                    }

                } else {
                    channel.sendMessage("API Key is invalid.").queue();
                }

Any help is appreciated, I at least want to know what I am doing wrong.

Thanks!

It has to do with relative paths. Try changing the filename in Main for the absolute path, something like "/tmp/apikeys.ini", to check that your code works correctly. If that works, then you can now change it to either something like "../../directory/filename" or something relative to where you know you're executing your code from. Get familiar with your java path environment variables such as JAVA_HOME and all to get it to something more permanent and portable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM