001    /*
002     * Copyright 2005,2009 Ivan SZKIBA
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.ini4j.tutorial;
017    
018    import org.ini4j.Wini;
019    
020    import org.ini4j.sample.Dwarf;
021    import org.ini4j.sample.Dwarfs;
022    
023    import org.ini4j.test.DwarfsData;
024    import org.ini4j.test.Helper;
025    
026    import static org.junit.Assert.*;
027    
028    import java.io.File;
029    import java.io.FileInputStream;
030    import java.io.FileOutputStream;
031    import java.io.IOException;
032    
033    //<editor-fold defaultstate="collapsed" desc="apt documentation">
034    //|
035    //|                -------------------
036    //|                One minute Tutorial
037    //|
038    //|One minute Tutorial - First step
039    //|
040    //| First step with \[ini4j\] library. No data model, no interfaces, no design
041    //| patterns, simply read and write windows .ini files.
042    //|
043    //</editor-fold>
044    public class OneMinuteTutorial extends AbstractTutorial
045    {
046        public static void main(String[] args) throws Exception
047        {
048            new OneMinuteTutorial().run(filearg(args));
049        }
050    
051        protected void copy(File inputFile, File outputFile) throws IOException
052        {
053            FileInputStream is = new FileInputStream(inputFile);
054            FileOutputStream os = new FileOutputStream(outputFile);
055            byte[] buff = new byte[8192];
056            int n;
057    
058            while ((n = is.read(buff)) > 0)
059            {
060                os.write(buff, 0, n);
061            }
062    
063            is.close();
064            os.close();
065        }
066    
067        @Override protected void run(File arg) throws Exception
068        {
069            File file = File.createTempFile("tutorial", ".ini");
070    
071            file.deleteOnExit();
072            copy(arg, file);
073            sample01(file.getCanonicalPath());
074            sample02(file.getCanonicalPath());
075        }
076    
077    //|
078    //| Lets read some value from .ini file...
079    //|
080    //{
081        void sample01(String filename) throws IOException
082        {
083            Wini ini = new Wini(new File(filename));
084            int age = ini.get("happy", "age", int.class);
085            double height = ini.get("happy", "height", double.class);
086            String dir = ini.get("happy", "homeDir");
087    
088    //}
089    //| ... assuming there is a section with name <<<happy>>>, which contains at least
090    //| the following options: <<<age>>>, <<<height>>> and <<<homeDir>>>, something like
091    //| this:
092    //|
093    //|+---------+
094    //| [happy]
095    //| age = 99
096    //| height = 77.66
097    //| homeDir = /home/happy
098    //|+---------+
099    //|
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    }