1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| package org.ini4j; |
18 |
| |
19 |
| import java.io.*; |
20 |
| import java.util.prefs.*; |
21 |
| |
22 |
| public class IniFile extends IniPreferences |
23 |
| { |
24 |
| public static enum Mode {RO,WO,RW}; |
25 |
| |
26 |
| private Mode _mode; |
27 |
| private File _file; |
28 |
| |
29 |
6
| public IniFile(File file, Mode mode) throws BackingStoreException
|
30 |
| { |
31 |
6
| super(new Ini());
|
32 |
6
| _file = file;
|
33 |
6
| _mode = mode;
|
34 |
| |
35 |
6
| if ( (_mode == Mode.RO) || ( (_mode != Mode.WO) && _file.exists() ) )
|
36 |
| { |
37 |
4
| sync();
|
38 |
| } |
39 |
| } |
40 |
| |
41 |
1
| public IniFile(File file) throws BackingStoreException
|
42 |
| { |
43 |
1
| this(file, Mode.RO);
|
44 |
| } |
45 |
| |
46 |
1
| public Mode getMode()
|
47 |
| { |
48 |
1
| return _mode;
|
49 |
| } |
50 |
| |
51 |
1
| public File getFile()
|
52 |
| { |
53 |
1
| return _file;
|
54 |
| } |
55 |
| |
56 |
6
| public void sync() throws BackingStoreException
|
57 |
| { |
58 |
6
| if ( _mode == Mode.WO )
|
59 |
| { |
60 |
1
| throw new BackingStoreException("write only instance");
|
61 |
| } |
62 |
| |
63 |
5
| try
|
64 |
| { |
65 |
5
| synchronized (lock)
|
66 |
| { |
67 |
5
| getIni().load(_file.toURL());
|
68 |
| } |
69 |
| } |
70 |
| catch (Exception x) |
71 |
| { |
72 |
1
| throw new BackingStoreException(x);
|
73 |
| } |
74 |
| } |
75 |
| |
76 |
3
| public void flush() throws BackingStoreException
|
77 |
| { |
78 |
3
| if ( _mode == Mode.RO )
|
79 |
| { |
80 |
1
| throw new BackingStoreException("read only instance");
|
81 |
| } |
82 |
| |
83 |
2
| try
|
84 |
| { |
85 |
2
| synchronized (lock)
|
86 |
| { |
87 |
2
| getIni().store(new FileWriter(_file));
|
88 |
| } |
89 |
| } |
90 |
| catch (Exception x) |
91 |
| { |
92 |
1
| throw new BackingStoreException(x);
|
93 |
| } |
94 |
| } |
95 |
| } |