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.Wini;
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.*;
27
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32
33
34
35
36
37
38
39
40
41
42
43
44 public class OneMinuteTutorial extends AbstractTutorial
45 {
46 public static void main(String[] args) throws Exception
47 {
48 new OneMinuteTutorial().run(filearg(args));
49 }
50
51 protected void copy(File inputFile, File outputFile) throws IOException
52 {
53 FileInputStream is = new FileInputStream(inputFile);
54 FileOutputStream os = new FileOutputStream(outputFile);
55 byte[] buff = new byte[8192];
56 int n;
57
58 while ((n = is.read(buff)) > 0)
59 {
60 os.write(buff, 0, n);
61 }
62
63 is.close();
64 os.close();
65 }
66
67 @Override protected void run(File arg) throws Exception
68 {
69 File file = File.createTempFile("tutorial", ".ini");
70
71 file.deleteOnExit();
72 copy(arg, file);
73 sample01(file.getCanonicalPath());
74 sample02(file.getCanonicalPath());
75 }
76
77
78
79
80
81 void sample01(String filename) throws IOException
82 {
83 Wini ini = new Wini(new File(filename));
84 int age = ini.get("happy", "age", int.class);
85 double height = ini.get("happy", "height", double.class);
86 String dir = ini.get("happy", "homeDir");
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 assertEquals(DwarfsData.happy.age, age);
102 assertEquals(DwarfsData.happy.height, height, Helper.DELTA);
103 assertEquals(DwarfsData.happy.homeDir, dir);
104 }
105
106
107
108
109 void sample02(String filename) throws IOException
110 {
111 Wini ini = new Wini(new File(filename));
112
113 ini.put("sleepy", "age", 55);
114 ini.put("sleepy", "weight", 45.6);
115 ini.store();
116
117
118
119
120
121
122
123
124
125
126
127
128 assertEquals(55, (int) ini.get(Dwarfs.PROP_SLEEPY, Dwarf.PROP_AGE, int.class));
129 assertEquals(45.6, (double) ini.get(Dwarfs.PROP_SLEEPY, Dwarf.PROP_WEIGHT, double.class), Helper.DELTA);
130 }
131
132
133
134
135 }