2017-10-23 9 views
0

同じオプションキーを持つ複数のオプション値を含むセクションを持つINIファイルが必要です。 rwordsでは、iniファイルに配列を表現したいと思います。 .iniファイル複数のオプションをini4jでリストや配列に読み込む方法は?

[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 

follwoing Javaコードを取得していない私の問題は、私が使用のgetAll方法に応じて、唯一の最後の値が配列またはLISTEに読み込まれていることです私のオプションキー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(); 
} 

私はのgetAll、アレイに呼び出すと、すべてのオプションの値を得ることができると思いました。

答えて

0

https://stackoverflow.com/users/7345335/philippe-cerou質問Java ini4j - reading multiple options from .ini fileのおかげで。

彼は私に、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(); 
    } 
関連する問題