1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.ini4j.tutorial;
17
18 import org.ini4j.Ini;
19
20 import org.ini4j.sample.Dwarf;
21 import org.ini4j.sample.Dwarfs;
22
23 import org.ini4j.test.DwarfsData;
24 import org.ini4j.test.Helper;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28
29 import java.io.File;
30 import java.io.FileReader;
31 import java.io.IOException;
32
33 import java.util.Map;
34 import java.util.Set;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class IniTutorial extends AbstractTutorial
51 {
52 public static void main(String[] args) throws Exception
53 {
54 new IniTutorial().run(filearg(args));
55 }
56
57 @Override protected void run(File arg) throws Exception
58 {
59 Ini ini = new Ini(arg.toURI().toURL());
60
61 sample01(ini);
62 sample02(arg);
63 sample03(ini);
64 sample04(ini);
65 }
66
67
68
69
70
71
72
73
74 void sample01(Ini ini)
75 {
76 Ini.Section section = ini.get("happy");
77
78
79
80
81 String age = section.get("age");
82 String weight = section.get("weight");
83 String homeDir = section.get("homeDir");
84
85
86
87
88 Map<String, String> map = ini.get("happy");
89
90 age = map.get("age");
91 weight = map.get("weight");
92 homeDir = map.get("homeDir");
93
94
95 Set<String> sectionNames = ini.keySet();
96
97
98 Helper.assertEquals(DwarfsData.happy, section.as(Dwarf.class));
99 }
100
101
102
103
104
105
106
107
108
109
110
111 void sample02(File file) throws IOException
112 {
113 Ini ini = new Ini();
114
115 ini.load(new FileReader(file));
116
117
118
119
120 ini = new Ini(new FileReader(file));
121 File copy = File.createTempFile("sample", ".ini");
122
123 ini.store(copy);
124
125 ini = new Ini(copy);
126 Helper.assertEquals(DwarfsData.dwarfs, ini.as(Dwarfs.class));
127 copy.delete();
128 }
129
130
131
132
133
134
135
136
137
138 void sample03(Ini ini)
139 {
140 Ini.Section dopey = ini.get("dopey");
141
142
143 String weightRaw = dopey.get("weight");
144 String heightRaw = dopey.get("height");
145
146
147 String weight = dopey.fetch("weight");
148 String height = dopey.fetch("height");
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 assertEquals(DwarfsData.INI_DOPEY_WEIGHT, weightRaw);
168 assertEquals(DwarfsData.INI_DOPEY_HEIGHT, heightRaw);
169 assertEquals(String.valueOf(DwarfsData.dopey.weight), weight);
170 assertEquals(String.valueOf(DwarfsData.dopey.height), height);
171 }
172
173
174
175
176
177
178
179
180
181 void sample04(Ini ini)
182 {
183 Ini.Section sneezy = ini.get("sneezy");
184 String n1 = sneezy.get("fortuneNumber", 0);
185 String n2 = sneezy.get("fortuneNumber", 1);
186 String n3 = sneezy.get("fortuneNumber", 2);
187 String n4 = sneezy.get("fortuneNumber", 3);
188
189
190 int[] n = sneezy.getAll("fortuneNumber", int[].class);
191
192
193
194 assertEquals("11", n1);
195 assertEquals("22", n2);
196 assertEquals("33", n3);
197 assertEquals("44", n4);
198 assertEquals(4, n.length);
199 assertEquals(11, n[0]);
200 assertEquals(22, n[1]);
201 assertEquals(33, n[2]);
202 assertEquals(44, n[3]);
203 }
204
205
206
207
208
209
210
211
212
213 void sample05()
214 {
215 Ini ini = new Ini();
216
217
218 ini.add("root/child/sub");
219
220
221 Ini.Section root;
222 Ini.Section sec;
223
224 root = ini.get("root");
225 sec = root.getChild("child").getChild("sub");
226
227
228 sec = root.lookup("child", "sub");
229
230
231 sec = root.lookup("child/sub");
232
233
234 sec = ini.get("root/child/sub");
235
236
237
238
239 assertNotNull(root.lookup("child", "sub"));
240 assertNotNull(ini.get("root/child"));
241 }
242 }