Java's operation of Properties configuration files
/ * * Created on 2005-1-4 * * Todo to change the Template for this generated file go to * window - preferences - java - code style - code templates * / package com.yorsun;
Import java.io.fileinputStream; import java.io.filenotfoundException; import java.io.Properties; import java.IOEXCEPTION; Import Java.UTIL.Properties
Import javax.servlet.servletContext; import javax.servlet.http.httpservlet;
/ ** * @author Administrator * * TODO To change the template for this generated type comment go to Window - * Preferences - Java - Code Style - Code Templates * / public class PropertiesUnit {private String filename;
Private property p;
PRIVATEINPUTSTREAM IN
PRIVATE FileoutputStream out;
Public Properties {this.FileName = filename; file file = new file (file file); try1 {in = new fileinputstream (file); p = new profment (); p.Load (in); in. close );} Catch (filenotfoundexception e) {// Todo auto-generated catch block system.err.println ("Profile config.properties can't find!"); E.PrintStackTrace ();} catch (ooException e) {/ / TODO Auto-Generated Catch Block System.err.Println ("Read Profile Config.Properties Error!"); E.PrintStackTrace ();}}
Public Static String getConfigfile (httpservlet HS) {Return getConfigfile (HS, "config.properties");}
/ ** * @param hs * @param configFileName * @return configFile * / private static String getConfigFile (HttpServlet hs, String configFileName) {String configFile = ""; sc = hs.getServletContext ServletContext (); configFile = sc.getRealPath ( "/" configfilename); if (configfile == null || configfile.equals (")) {configfile =" / " configFileName;} // Todo auto-generated method stub return configfile;} public void list () {P.list (system.out);
Public String getValue (String ItemName) {Return P.GetProperty (itemname);
Public String getValue (string itemname, string defaultvalue) {Return P.GetProperty (itemname, defaultValue);
Public void setValue (string value) {p.setproperty (itemname, value);
public void saveFile (String filename, String description) throws Exception {try {File f = new File (filename); out = new FileOutputStream (f); p.store (out, description); out.close ();} catch ( IOException ex) {throw new Exception ( "Unable to save the specified configuration file:" filename);}} public void saveFile (String filename) throws Exception {saveFile (filename, "");} public void saveFile () throws Exception { IF (filename.length () == 0) Throw new exception ("You need to specify a saved configuration file name"); savefile (filename);} public void deletevalue (string value) {p.Remove (value);} public static Void main (string args []) {string file = "/ eclipse / workspace / newstest / web-inf / config.properties"; // string file = "D: // Eclipse // Workspace // newstest // web- INF // Config.properties "; PropertiesUnit Pu = New PropertiesUnit (file); pu.list ();
}