KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > yagga > util > AssociativeFile


1 /*
2  * This file is part of MiniInstaller, a self installer builder for Java
3  * Copyright (C) 2002 Walter Gamba
4  * mailto:walter@yagga.net
5  * http://www.yagga.net/java/miniinstaller
6  *
7  * MiniInstaller is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * MiniInstaller is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  *
21  * As the time of writing, the GNU General Public Licene can be
22  * found at http://www.gnu.org/licenses/gpl.txt
23  *
24  */

25
26 package net.yagga.util;
27
28 import java.util.*;
29 import net.yagga.util.Ut;
30 import java.io.*;
31 /**
32  * AssociativeFile.
33
34     A file containings pairs key = value
35     key can have spaces
36     Comments in the form #, // or as usual C-like /* ...
37     @author Walter Gamba
38 */

39
40 public class AssociativeFile extends AssociativeReader
41 {
42     public static void main(String JavaDoc[] argv){
43         AssociativeFile aa=new AssociativeFile(argv[0]);
44         HashMap map=new HashMap();
45         aa.parseAssociative(map);
46         Iterator it=map.entrySet().iterator();
47         while(it.hasNext()){
48             Map.Entry me=(Map.Entry)it.next();
49             System.out.println("<"+me.getKey()+">='"+me.getValue()+"'");
50         }
51     }
52
53     String JavaDoc associativeFile;
54
55     public AssociativeFile(String JavaDoc file)
56     {
57         init(file,false);
58     }
59     public AssociativeFile(String JavaDoc file, boolean autocreate)
60     {
61         init(file,autocreate);
62     }
63
64     void init(String JavaDoc file, boolean autocreate)
65     {
66         associativeFile=file;
67         if(autocreate){
68             File f=new File(file);
69             if(!f.exists()){
70                 PrintWriter p=Ut.openOut(associativeFile);
71                 p.close();
72             }
73             f=null;
74         }
75     }
76
77     public boolean parseAssociative(HashMap map)
78     {
79         //Reader r=new InputStreamReader(Ut.openIn(associativeFile));
80
Reader r=new InputStreamReader(ResourceMgr.openResource(associativeFile));
81     return super.parseAssociativeReader(map,r);
82     }
83
84
85     public void write(HashMap map){
86         PrintWriter p=Ut.openOut(associativeFile);
87         Iterator it=map.entrySet().iterator();
88         while(it.hasNext()){
89             Map.Entry me=(Map.Entry)it.next();
90             p.println(me.getKey()+"="+me.getValue());
91         }
92         p.close();
93     }
94 }
Popular Tags