0

I want to have an INI file with a section containing a bunch of option values with the same option key. In othe rwords, I want to represent an array in the ini file. My problem is that only the last value is read into the array or liste, depending on the getAll method I use:

The .ini file:

[FTP]
; Access FTP server?
active = false
file.pattern = VA_.*.(csv|dat)$
#file.pattern = VA_.*(\\.(?i)(csv|dat))$
delete.after.download = false

[SFTP]
; Access FTP server?
active = true
file.pattern = VA_.*.(csv|dat)$
#file.pattern = VA_.*(\\.(?i)(csv|dat))$
delete.after.download = false

[SMB]
; Access SMB target?
active = false

[SCP]
; Access SCP target?
active = false

[FTP_Accounts]
ftpAccount = /aaa/xxx
ftpAccount = /bbb/xxx
ftpAccount = /ccc/xxx
ftpAccount = /ddd/xxx
ftpAccount = /eee/xxx
ftpAccount = /fff/xxx

The follwoing Java code doesn't get me all options values for option key ftpAccount:

public SftpFileHandler() {

    Wini ini = null;
    try {
        Config.getGlobal().setEscape(false);
        Config.getGlobal().setMultiSection(true);
        Config.getGlobal().setMultiOption(true);
        ini = new Wini(new File("MyIniFile.ini"));
    } catch (InvalidFileFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    final String ftpFileNamePattern =
            ini.get("FTP", "file.pattern", String.class);
    pattern = Pattern.compile(ftpFileNamePattern);

    List<Ini.Section> list = ini.getAll("FTP_Accounts");
    final Ini.Section ftpAccountsSection = ini.get("FTP_Accounts");
    for (final String optionKey: ftpAccountsSection.keySet()) {
        System.out.println(optionKey);
    }
    ftpAccounts = ftpAccountsSection.getAll("ftpAccount", String[].class);
    final List<String> ftpAccountsList = ftpAccountsSection.getAll("ftpAccount");
    final Ini.Section sftpAccountsSection = ini.get("SFTP_Accounts");
    sftpAccounts = sftpAccountsSection.getAll("sftpAccount", String[].class);

    connect();
}

I thought I could get all the option values with the getAll calls into an array.

4

1 回答 1

0

感谢https://stackoverflow.com/users/7345335/philippe-cerou有问题的Java ini4j - 从 .ini 文件中读取多个选项

他指出我在实例化 Wini 对象时不要加载 ini 文件。首先创建一个 Config 实例并将其MultiOption属性设置为 true。然后初始化一个没有ini文件作为参数的Wini实例。而是在之后使用load()方法加载 ini 文件。

Wini ini = null;
    Config conf = new Config();
    try {
        conf.setMultiOption(true);
        ini = new Wini();
        ini.setConfig(conf);
        ini.load(new File("MyIniFile.ini"));
    } catch (InvalidFileFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
于 2017-10-23T20:34:10.730 回答