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.Reg;
19  
20  import static org.junit.Assert.*;
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  //<editor-fold defaultstate="collapsed" desc="apt documentation">
26  //|
27  //|                -------------------------
28  //|                Windows Registry Tutorial
29  //|
30  //|Windows Registry Tutorial - Read/Write windows registry
31  //|
32  //| Yes, it is possible now to read/write registry from java programs
33  //| without native (JNI) code !
34  //|
35  //</editor-fold>
36  public class WindowsRegistryTutorial extends AbstractTutorial
37  {
38      public static final String FILENAME = "../sample/dwarfs.reg";
39  
40      public static void main(String[] args) throws Exception
41      {
42          if (Reg.isWindows())
43          {
44              new WindowsRegistryTutorial().run(filearg(args));
45          }
46      }
47  
48      @Override protected void run(File arg) throws Exception
49      {
50          sample01();
51          sample02();
52          sample03();
53      }
54  
55  //|
56  //|* Write
57  //|
58  //| Lets write something to registry
59  //{
60      void sample01() throws IOException
61      {
62          Reg reg = new Reg();
63          Reg.Key key = reg.add("HKEY_CURRENT_USER\\hello");
64  
65          key.put("world", "Hello World !");
66          reg.write();
67  //}
68  //| This code will create a "hello" key in HKEY_CURRENT_USER hive, and
69  //| put "Hello World !" with name "world".
70      }
71  
72  //|
73  //|* Read
74  //|
75  //| Lets read something from Control Panel settings...
76  //{
77      void sample02() throws IOException
78      {
79          Reg reg = new Reg("HKEY_CURRENT_USER\\Control Panel");
80          Reg.Key cp = reg.get("HKEY_CURRENT_USER\\Control Panel");
81          Reg.Key sound = cp.getChild("Sound");
82          String beep = sound.get("Beep");
83  
84  //}
85  //|
86      }
87  
88  //|
89  //|* Create environment variable
90  //|
91  //| Lets create a new environment variable under current users environment....
92  //{
93      void sample03() throws IOException
94      {
95          Reg reg = new Reg();
96          Reg.Key env = reg.add("HKEY_CURRENT_USER\\Environment");
97  
98          env.put("SAMPLE_HOME", "c:\\sample");
99          reg.write();
100 //}
101 //| Thats it ! Now your environment contains variable SAMPLE_HOME ! Unfortunetly
102 //| you have to restart Windows to see this variable.... but hey, we crated new
103 //| environment variable from java without any native code !
104     }
105 }