001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: ConfigFile.java,v $ 023 Revision 1.5 2004/05/05 21:36:35 markl 024 comment block updates 025 026 Revision 1.4 2003/01/19 09:37:12 markl 027 Javadoc & comment header updates. 028 029 Revision 1.3 2001/03/12 01:58:41 markl 030 Source code cleanup. 031 032 Revision 1.2 1999/01/10 03:34:00 markl 033 added GPL header & RCS tag 034 ---------------------------------------------------------------------------- 035*/ 036 037package kiwi.io; 038 039import java.awt.Color; 040import java.io.*; 041import java.util.*; 042 043import kiwi.text.*; 044import kiwi.util.Config; 045 046/** Persistent configuration object. This class extends 047 * <code>kiwi.util.Config</code>, adding convenience methods for saving a 048 * property list to a file, and reading a property list from a file. 049 * 050 * @see java.util.Properties 051 * @see kiwi.util.Config 052 * 053 * @author Mark Lindner 054 */ 055 056public class ConfigFile extends Config 057 { 058 private File file = null; 059 060 /** Construct a new <code>ConfigFile</code>. Note that the object has to be 061 * initialized by explicitly loading the properties via a call to 062 * <code>load()</code>; the constructor does not preload the file. 063 * 064 * @param file The <code>File</code> object for this configuration file. 065 * @param comment The top-of-file comment (one line). 066 */ 067 068 public ConfigFile(File file, String comment) 069 { 070 super(comment); 071 this.file = file; 072 } 073 074 /** Construct a new <code>ConfigFile</code> with a default comment. Note that 075 * the object has to be initialized by explicitly loading the properties 076 * via a call to <code>load()</code>; the constructor does not preload the 077 * file. 078 * 079 * @param file The <code>File</code> object for this configuration file. 080 */ 081 082 public ConfigFile(File file) 083 { 084 this(file, null); 085 } 086 087 /** Load the configuration parameters from the file. Also fires a 088 * <code>ChangeEvent</code> to notify listeners that the object 089 * (potentially) changed. 090 * 091 * @exception java.io.FileNotFoundException If the associated file does not 092 * exist. 093 * @exception java.io.IOException If the file could not be read. 094 * @see #store 095 */ 096 097 public void load() throws FileNotFoundException, IOException 098 { 099 FileInputStream fin = new FileInputStream(file); 100 super.load(fin); 101 fin.close(); 102 support.fireChangeEvent(); 103 } 104 105 /** Save the configuration parameters to the file. 106 * 107 * @exception java.io.IOException If the file could not be written. 108 * @see #load 109 */ 110 111 public void store() throws IOException 112 { 113 FileOutputStream fout = new FileOutputStream(file); 114 super.store(fout, description); 115 fout.close(); 116 } 117 118 /** Get the absolute path of this configuration file. */ 119 120 public String getPath() 121 { 122 return(file.getAbsolutePath()); 123 } 124 125 } 126 127/* end of source file */