Wini 简明教程

Wini 简明教程The ini4j isasimpleJav iniformat Additionally thelibraryin

大家好,欢迎来到IT知识分享网。

使用ini4j库第一步

 void sample01(String filename) throws IOException { Wini ini = new Wini(new File(filename)); int age = ini.get("happy", "age", int.class); double height = ini.get("happy", "height", double.class); String dir = ini.get("happy", "homeDir"); }

假设有一个以happy命名的节,它至少包含以下选项:ageheight以及homeDir

[happy] age = 99 height = 77.66 homeDir = /home/happy

现在来看如何写入值:

 void sample02(String filename) throws IOException { Wini ini = new Wini(new File(filename)); ini.put("sleepy", "age", 55); ini.put("sleepy", "weight", 45.6); ini.store(); }

之后,文件会有一个sleepy节,并且这个节将会包含两个选项:值为55的age和值为45.6的weight,就像这样:

 [sleepy] age = 55 weight = 45.6

如何使用[ini4j] API

这个教程中的代码片段使用了.ini文件dwarfs.ini测试通过。

数据模型(Data model)

.init文件的数据模型由org.ini4j.Init 类表示。这个类实现了Map<String,Section>。这意味着你可以使用java.util.Map集合API接口访问section(节)。节是一个实现了Map<String, String>的Map。

 void sample01(Ini ini) { Ini.Section section = ini.get("happy"); // // read some values // String age = section.get("age"); String weight = section.get("weight"); String homeDir = section.get("homeDir"); // // .. or just use java.util.Map interface... // Map<String, String> map = ini.get("happy"); age = map.get("age"); weight = map.get("weight"); homeDir = map.get("homeDir"); // get all section names Set<String> sectionNames = ini.keySet(); }

加载和存储数据(Loading and storing data)

有多个方法可以把数据加载到Ini对象。加载可以通过load方法或者重载的构造方法。数据可以从InputStreamReaderURL或者File加载。

你可以使用store方法存储数据。数据可以被存储到OutputStreamWriter或者File

 void sample02(File file) throws IOException { Ini ini = new Ini(); ini.load(new FileReader(file)); // // or instantiate and load data: // ini = new Ini(new FileReader(file)); File copy = File.createTempFile("sample", ".ini"); ini.store(copy); }

宏/变量的替换(Macro/variable substitution)

要获取值,除了get(),你还可以使用fetch(),这个方法可以用解决任何正在发生的${section/option}格式变量的引用。

 void sample03(Ini ini) { Ini.Section dopey = ini.get("dopey"); // get method doesn't resolve variable references String weightRaw = dopey.get("weight"); // = ${bashful/weight} String heightRaw = dopey.get("height"); // = ${doc/height} // to resolve references, you should use fetch method String weight = dopey.fetch("weight"); // = 45.7 String height = dopey.fetch("height"); // = 87.7 }

假设我们有一个使用如下节的.ini文件:

 [dopey] weight = ${bashful/weight} height = ${doc/height} [bashful] weight = 45.7 height = 98.8 [doc] weight = 49.5 height = 87.7

多值(Multi values)

[ini4j]库介绍了MultiMap接口,虽然它继承了正常的Map,但是对于每个键(key)允许有多个值(values)。你可以就根据给出的一个键(key)来索引值,类似JavaBeans API索引的属性。

 void sample04(Ini ini) { Ini.Section sneezy = ini.get("sneezy"); String n1 = sneezy.get("fortuneNumber", 0); // = 11 String n2 = sneezy.get("fortuneNumber", 1); // = 22 String n3 = sneezy.get("fortuneNumber", 2); // = 33 String n4 = sneezy.get("fortuneNumber", 3); // = 44 // ok, lets do in it easier... int[] n = sneezy.getAll("fortuneNumber", int[].class); }

树模型(Tree Model)

如果超过两层映射模型,Ini类则提供了树模型。你可以像树一样访问节(Sections)。这意味着节(section)成为路径名,使用一个路径分隔字符(’/’和’\ ‘的Wini和注册。不知道这句话什么怎么翻译,囧,原文是:”’/’ and ‘\’ on Wini and Reg”)。

 void sample05() { Ini ini = new Ini(); // lets add a section, it will create needed intermediate sections as well ini.add("root/child/sub"); // Ini.Section root; Ini.Section sec; root = ini.get("root"); sec = root.getChild("child").getChild("sub"); // or... sec = root.lookup("child", "sub"); // or... sec = root.lookup("child/sub"); // or even... sec = ini.get("root/child/sub"); }

如果你正在使用Wini,而不是Ini类,那么路径分隔符就是’\’.

注册表教程-Windows .REG file handling

加载和存储

加载和存储数据没有什么特别的,非常类似Ini类。但是在加载数据时,Reg类会剥离.REG中的特别的值(字符串周围的双引号,来自option的类型数据等)。所以在加载.REG文件后,你可以想使用Ini类一样。当然如果你存储Reg类,它会把以上所有的元信息放入到文件,所以结果会是有效的.REG文件。你不需要担心在第一行的文件编码,版本等。

假设你有一个.REG文件,像如下section/key:

[HKEY_CURRENT_USER\Software\ini4j-test\dwarfs\bashful] @="bashful" "weight"=hex(2):34,00,35,00,2e,00,37,00,00,00 "height"="98.8" "age"=dword:00000043 "homePage"="http://snowwhite.tale/~bashful" "homeDir"="/home/bashful"

正如你看到的,”weight”和”see”不是简单的字符串。”height”是一个REG_DWORD类型,但是”weight”是REG_EXPAND_SZ。不要担心,Reg类会关心类型转换,你只要像常规的.ini文件访问即可:

 void sample01(File file) throws IOException { Reg reg = new Reg(file); Reg.Key hive = reg.get(Reg.Hive.HKEY_CURRENT_USER.toString()); Reg.Key bashful; bashful = hive.lookup("Software", "ini4j-test", "dwarfs", "bashful"); // or ... bashful = hive.lookup("Software\\ini4j-test\\dwarfs\\bashful"); // or even... bashful = reg.get("HKEY_CURRENT_USER\\Software\\ini4j-test\\dwarfs\\bashful"); // read some data double weight = bashful.get("weight", double.class); // = 45.7 double height = bashful.get("height", double.class); // = 98.8 int age = bashful.get("age", int.class); // = 67 URI homePage = bashful.get("homePage", URI.class); // = new URI("http://snowwhite.tale/~bashful"); String homeDir = bashful.get("homeDir"); // = "/home/bashful"}

Types

当你加载数据到Reg类时,它会保留元信息,比如类型信息。如果你创建新的值,默认会有类型REG_SZ。当然你还可以为值指定类型信息。

 void sample02() { Reg reg = new Reg(); Reg.Key key = reg.add("HKEY_CURRENT_USER\\Software\\ini4j-test\\dwarfs\\sleepy"); key.put("fortuneNumber", 99); key.putType("fortuneNumber", Reg.Type.REG_MULTI_SZ);

如果你存储reg对象,它会包含节,类似这个:

[HKEY_CURRENT_USER\Software\ini4j-test\dwarfs\sleepy] "fortuneNumber"=hex(7):39,00,39,00,00,00,00,00

Windows Registry Tutorial – Read/Write windows registry

是的,从Java程序中,不用本地(JNI)代码可以读写注册表。

写(Write)

写一些东西到注册表中。

 void sample01() throws IOException { Reg reg = new Reg(); Reg.Key key = reg.add("HKEY_CURRENT_USER\\hello"); key.put("world", "Hello World !"); reg.write();

这个代码会在HKEY_CURRENT_USER hive中创建一个”hello”的键,并且把”Hello World”放入,命名为”world”.

读(Read)

从Control Panel settings读一些东西。

 void sample02() throws IOException { Reg reg = new Reg("HKEY_CURRENT_USER\\Control Panel"); Reg.Key cp = reg.get("HKEY_CURRENT_USER\\Control Panel"); Reg.Key sound = cp.getChild("Sound"); String beep = sound.get("Beep");

创建环境变量(Create environment variable)

在当前用户环境中创建一个新的环境变量。

 void sample03() throws IOException { Reg reg = new Reg(); Reg.Key env = reg.add("HKEY_CURRENT_USER\\Environment"); env.put("SAMPLE_HOME", "c:\\sample"); reg.write();

现在,当前用户的环境变量中会包含变量”SAMPLE_HOME”。不幸的是你必须重启Windows才能看到这个变量。但是我们确实在Java程序中没有使用任何本地代码而创建了一个新的环境变量。

Bean Tutorial – Using your own API !

是的,可以做到。要访问sections中的内容,你可以使用任何你自定义的兼容API的Bean。为了做到这点,你必须创建一个Java Bean风格接口或者类。

Accessing sections as beans

在写程序时,通常我们知道sections的类型,所以我们定义一个或者多个Java接口去访问它们。这种方案的一个优点是程序员不一定要转换值,因为它们会被自动转换成接口中的类型。

当然,你可能还使用setter,不止getters。通过这个办法,你们安全地改变值的类型。

 void sample01(Ini ini) { Ini.Section sec = ini.get("happy"); Dwarf happy = sec.as(Dwarf.class); int age = happy.getAge(); URI homePage = happy.getHomePage(); happy.setWeight(45.55);

Marshalling beans

有时候我们想存储已存在的Java Beans在文本文件里。这个操作通常被称为marshalling。

使用[ini4j],可以很轻易地存储bean的属性到一个section中。你可以简单地创建一个section,并且调用section的from()方法。

 void sample02(Ini ini) { DwarfBean sleepy = new DwarfBean(); sleepy.setAge(87); sleepy.setWeight(44.3); Ini.Section sec = ini.add("sleepy"); sec.from(sleepy);

Unmarshalling beans

 void sample03(Ini ini) { DwarfBean grumpy = new DwarfBean(); ini.get("grumpy").to(grumpy);

Indexed properties

为了处理索引属性,你应该在配置中允许多个选项值处理。允许这个特点后,选项可能包含多个值(在文件里多行)。这些值可以被映射到索引bean属性中。

 void sample04(URL location) throws IOException { Config cfg = new Config(); cfg.setMultiOption(true); Ini ini = new Ini(); ini.setConfig(cfg); ini.load(location); Ini.Section sec = ini.get("sneezy"); Dwarf sneezy = sec.as(Dwarf.class); int[] numbers = sneezy.getFortuneNumber(); // // same as above but with unmarshalling... // DwarfBean sneezyBean = new DwarfBean(); sec.to(sneezyBean); numbers = sneezyBean.getFortuneNumber();

Options

不仅仅IniIni.Section有bean接口。有一个为OptionMap的bean接口,以及每个Option派生类。Options是一个java.util.properties加强版替代。

void sample05(Options opts) { Dwarf dwarf = opts.as(Dwarf.class); int age = dwarf.getAge(); // // same as above but with unmarshalling // DwarfBean dwarfBean = new DwarfBean(); opts.to(dwarfBean); age = dwarfBean.getAge();

在上面的例子中,顶层properties(像”age”)被映射为一个bean属性。

首选教程(Preferences Tutorial)

这篇文档的目的是让读者熟悉[ini4j]库的Preference接口的使用。每章包含了所有必要的代码部分以及给出功能的解释。

这个教程里的代码片段使用.ini file: dwarfs.ini测试通过。

只要Preferences对象被创建,那么它就能够像标准Preference一样,并且应该像这样被使用。在根节点里,必须显示地创建一个唯一的新节点(nodes)。在第一个节点(nodes或者sections)里,只有值被创建(将会有options)。

在无效操作情况下,会产生一个UnsupportedOperationException 类型的运行时异常。如果我们试图在根节点(node)或者在第二个节点(node)上要设置一个值时会发生,因为这些操作无法在Preference下整个.ini被解释。

Reading and writing values

值可以像其他Preference一样被读写,没有区别。

 void sample01(Ini ini) throws IOException { Preferences prefs = new IniPreferences(ini); Preferences bashful = prefs.node("bashful"); String home = bashful.get("homeDir", "/home"); int age = bashful.getInt("age", -1); bashful.putDouble("weight", 55.6);

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/138916.html

(0)
上一篇 2025-06-09 22:00
下一篇 2025-06-09 22:10

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信