Question

I'm using the ini4j API http://ini4j.sourceforge.net/

So far I am able to read from an ini file using a JFileChooser which populates the fields required; I need to be able to grab the content of those text fields (as Strings) and write them to an ini file using the API.

The code I have so far is:

public void writeIniFile() throws IOException{
    JFileChooser saveinijfc = new JFileChooser(currdir);
    int savereturnval = saveinijfc.showSaveDialog(null);
    if (savereturnval == JFileChooser.APPROVE_OPTION){
        File inioutfile = new File(saveinijfc.getSelectedFile(), "");
        Wini ini = new Wini(new File(inioutfile.getAbsolutePath()));

            // [GLOBAL]
            ini.put("GLOBAL", "clientname", getClientnameText());

            // [PREBACKUP]
            ini.put("PREBACKUP", "prebackup-enabled", String.valueOf(getPrebackupenabledSelection()));

            // [OPENVPN]
            ini.put("OPENVPN", "ovpnprofilename", getOvpnprofileText());

            ini.put("OPENVPN", "remotegatewayip", getOpenvpngatewayipText());

            // [NETWORK DRIVE]
            ini.put("NETWORK DRIVE", "drive-letter", getNetworkdriveletterText());

            ini.put("NETWORK DRIVE", "ipofshare", getIpofshareText());

            ini.put("NETWORK DRIVE", "sharename", getSharenameText());

            ini.put("NETWORK DRIVE", "shareusername", getShareusernameText());

            ini.put("NETWORK DRIVE", "sharepassword", getSharepasswordText());

            // [REGISTRY BACKUP]
            ini.put("REGISTRY BACKUP", "registrybackup-enabled", String.valueOf(getRegistrybackupenabledSelection()));

            ini.put("REGISTRY BACKUP", "hklm-software", String.valueOf(getHklmsoftwareenabledSelection()));

            ini.put("REGISTRY BACKUP", "reg-custompath-enabled", getRegistrycustompathenabledSelection());

            ini.put("REGISTRY BACKUP", "reg-custom-path", getRegistrycustompathText());

            // [EMAIL]
            ini.put("EMAIL", "emailenabled", String.valueOf(getEmailenabledSelection()));

            ini.put("EMAIL", "gmailusername", getGmailusernameText());

            ini.put("EMAIL", "gmailpassword", getGmailpasswordText());

            ini.put("EMAIL", "clientemail", getClientemailText());

            // STORE INI FILE
            ini.store();
    }

}

I suspect I need to create some output streams to write the file, I was following this tutorial http://ini4j.sourceforge.net/tutorial/OneMinuteTutorial.java.html for reference.

Was it helpful?

Solution

Why not just call:

File inioutfile = new File(saveinijfc.getSelectedFile(), "");
//Insert here.
if(!inioutfile.exists()) {
    if(!inioutfile.createNewFile()) return;
}
Wini ini = new Wini(new File(inioutfile.getAbsolutePath()));

This should ensure that the file does exist, if it doesn't it'll make it, and if it can't make it it will exit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top