1

我正在使用 ini4j API http://ini4j.sourceforge.net/

到目前为止,我能够使用填充所需字段的 JFileChooser 从 ini 文件中读取;我需要能够获取这些文本字段的内容(作为字符串)并使用 API 将它们写入 ini 文件。

我到目前为止的代码是:

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();
    }

}

我怀疑我需要创建一些输出流来编写文件,我正在关注本教程http://ini4j.sourceforge.net/tutorial/OneMinuteTutorial.java.html以供参考。

4

1 回答 1

1

为什么不直接打电话:

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

这应该确保该文件确实存在,如果不存在,它将成功,如果无法成功,它将退出。

于 2013-02-10T12:52:31.880 回答