View Javadoc

1   /*
2    * Copyright 2005,2009 Ivan SZKIBA
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  //<editor-fold defaultstate="collapsed" desc="apt documentation">
34  //|
35  //|                -------------------
36  //|                One minute Tutorial
37  //|
38  //|One minute Tutorial - First step
39  //|
40  //| First step with \[ini4j\] library. No data model, no interfaces, no design
41  //| patterns, simply read and write windows .ini files.
42  //|
43  //</editor-fold>
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  //| Lets read some value from .ini file...
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  //| ... assuming there is a section with name <<<happy>>>, which contains at least
90  //| the following options: <<<age>>>, <<<height>>> and <<<homeDir>>>, something like
91  //| this:
92  //|
93  //|+---------+
94  //| [happy]
95  //| age = 99
96  //| height = 77.66
97  //| homeDir = /home/happy
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 //| Now let see how to write values....
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 //| ... and then file will have a section <<<sleepy>>> and this section
119 //| will contains at least two options: <<<age>>> with value <<<55>>> and <<<weight>>>
120 //| with value <<<45.6>>>, something like this:
121 //|
122 //|+---------+
123 //| [sleepy]
124 //| age = 55
125 //| weight = 45.6
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 //| If you want to know more about this library, read
134 //| {{{../tutorial/index.html}tutorials}}
135 }