【问题标题】:ini4j - How to get all the key names in a setting?ini4j - 如何获取设置中的所有键名?
【发布时间】:2009-10-21 17:19:31
【问题描述】:

我决定使用 ini 文件来为我的 Java 应用程序存储简单的键值对配置。

我搜索并搜索了 stackoverflow,发现强烈建议使用 ini4j 来解析和解释 Java 中的 ini 文件。我花了一些时间阅读 ini4j 网站上的教程;但是,我不确定如何获取 ini 文件中设置的所有键值。

例如,如果我有这样的 ini 文件:

[ food ]
name=steak
type=american
price=20.00

[ school ]
dept=cse
year=2
major=computer_science

并假设我事先不知道键的名称。如何获取键列表以便最终可以根据键检索值?例如,如果我得到食物的键列表,我会得到一个数组或某种包含“名称”、“类型”和“价格”的数据结构。

谁能给我一个例子,你可以打开一个 ini 文件,解析或解释它,以便应用知道 ini 文件的所有结构和值,并获取键和值列表?

【问题讨论】:

  • 您可能更愿意使用 java.util.Properties,尽管它没有像 .ini 文件那样的部分。
  • @mmyers:这就是问题所在。我需要我正在制作的这个工具的部分。我很确定有一种方法可以用 ini4j 做到这一点,只是找不到合适的 api、类或教程

标签: java configuration ini4j


【解决方案1】:

对此没有任何保证。 5分钟就搞定了。 但它会读取您提供的 ini,而无需进一步了解 ini 本身(除了知道它由多个部分组成,每个部分都有多个选项。

猜猜你必须自己解决剩下的问题。

import org.ini4j.Ini;
import org.ini4j.Profile.Section;
import java.io.FileReader;

public class Test {
    public static void main(String[] args) throws Exception {
        Ini ini = new Ini(new FileReader("test.ini"));
        System.out.println("Number of sections: "+ini.size()+"\n");
        for (String sectionName: ini.keySet()) {
            System.out.println("["+sectionName+"]");
            Section section = ini.get(sectionName);
            for (String optionKey: section.keySet()) {
                System.out.println("\t"+optionKey+"="+section.get(optionKey));
            }
        }
    }
}

也请查看ini4j Samplesini4j Tutorials。通常是一个没有很好记录的库。

【讨论】:

  • 我只是想出了如何做我需要做的任何事情!类似于您发布的解决方案!感谢您为此付出的努力。我必须通过 API 进行教育猜测才能找到有用的类 :)
【解决方案2】:

我在教程中找不到任何东西,所以我逐步浏览了源代码,直到找到entrySet 方法。有了它,你可以这样做:

Wini ini = new Wini(new File(...));
Set<Entry<String, Section>> sections = ini.entrySet(); /* !!! */

for (Entry<String, Section> e : sections) {
    Section section = e.getValue();
    System.out.println("[" + section.getName() + "]");

    Set<Entry<String, String>> values = section.entrySet(); /* !!! */
    for (Entry<String, String> e2 : values) {
        System.out.println(e2.getKey() + " = " + e2.getValue());
    }
}

此代码实质上是将 .ini 文件重新打印到控制台。您的示例文件会产生以下输出:(顺序可能会有所不同)

[food]
name = steak
type = american
price = 20.00
[school]
dept = cse
year = 2
major = computer_science

【讨论】:

    【解决方案3】:

    感兴趣的方法是get()和keySet()

    Wini myIni = new Wini (new File ("test.ini"));
    
    // list section names
    for (String sName : myIni.keySet()) {
        System.out.println(sName);
    }
    
    // check for a section, section name is case sensitive
    boolean haveFoodParameters = myIni.keySet().contains("food");
    
    // list name value pairs within a specific section
    for (String name : myIni.get("food").keySet() {
        System.out.println (name + " = " + myIni.get("food", name)
    }
    

    【讨论】:

      【解决方案4】:

      在 Kotlin 中:

              val ini = Wini(File(iniPath))
              Timber.e("Read value:${ini}")
              println("Number of sections: "+ini.size+"\n");
              for (sectionName in ini.keys) {
                  println("[$sectionName]")
                  val section: Profile.Section? = ini[sectionName]
                  if (section != null) {
                      for (optionKey in section.keys) {
                          println("\t" + optionKey + "=" + section[optionKey])
                      }
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-17
        • 1970-01-01
        • 2011-01-18
        相关资源
        最近更新 更多