Use of Properties classes in Java

xiaoxiao2021-03-06  42

Package com.Adrop.util;

Import java.io. *;

Import java.util.properties;

Import javax.servlet.http. *;

Import javax.servlet. *;

Import javax.servlet.jsp. *;

Public class protresutil {

PRIVATE STRING FILENAME;

Private property p;

PRIVATEINPUTSTREAM IN

PRIVATE Fileoutputstream out

/ **

* Load file according to the transfer file name

* @Param FileName String

* /

Public propertyTiesUTIL (String filename) {

THIS.FILENAME = filename;

File File = New File (filename);

Try {

IN = New fileInputstream (file);

P = new profment ();

// Load file

p.Load (in);

In.Close ();

}

Catch (filenotfoundexception e) {

System.err.Println ("Profile config.properties can't find !!");

E.PrintStackTrace ();

}

Catch (Exception E) {

System.err.Println ("Read Profile config.properties);

E.PrintStackTrace ();

}

}

/ **

* Profiles are all config.properties and are uniformly placed in the root directory of the web application.

* @Return String

* /

Public static string getconfigfile (httpservlet hs) {

Return getConfigfile (HS, "config.properties");

}

/ **

* Used in servlet, use this directly as the parameters, httpservlet type

* Find the configuration file from the root of the current web application based on the configuration file name

* @Param HS HTTPSERVLET

* @Param configFileName String Profile Name

* @Return String

* /

Public Static String getconfigfile (httpservlet HS, String configfilename) {

String configfile = "";

ServletContext sc = hs.getservletContext ();

Configfile = sc.getRealPath ("/" configfilename);

IF (configfile == null || configfile.equals (")) {

Configfile = "/" configFileName;

}

Return Configfile;

}

/ **

* JSP uses PageContext as a parameter

* @Param HS PageContext

* @Param configFileName String Profile Name

* @Return String

* /

Public Static String getconfigfile (pagecontext hs, string configfilename) {string configfile = ""

ServletContext sc = hs.getservletContext ();

Configfile = sc.getRealPath ("/" configfilename);

IF (configfile == null || configfile.equals (")) {

Configfile = "/" configFileName;

}

Return Configfile;

}

/ **

* List all configuration file content

* /

Public void list () {

p.List (System.out);

}

/ **

* Specify the configuration item name, return to the configuration value

* @Param ItemName String

* @Return String

* /

Public String getValue (string itemname) {

Return P.GetProperty (itemname);

}

/ **

* Specify the configuration item name and default value, return to the configuration value

* @Param ItemName String

* @Param DefaultValue String

* @Return String

* /

Public String getValue (String ItemName,

String defaultValue) {

Return P.getProperty (itemname, defaultValue);

}

/ **

* Set the configuration item name and its value

* @Param ItemName String

* @Param Value String

* /

Public void setvalue (string itemname, string value) {

P.SetProperty (itemName, Value);

Return;

}

/ **

* Save the configuration file, specify the file name and header description

* @Param FileName String

* @Param Description String

* @Throws Exception

* /

Public void savefile (string filename, string description) throws exception {

Try {

FILE F = New File (filename);

OUT

= New FileOutputStream (f);

P.Store (out, description); // Save File

Out.close ();

}

Catch (IOException EX) {

Throw new Exception

("Unable to save the specified configuration file:" filename);

}

}

/ **

* Save the configuration file, specify the file name

* @Param FileName String

* @Throws Exception

* /

Public void savefile (String filename)

Throws exception {

Savefile (filename, ");

}

/ **

* Save the configuration file, use the original text name

* @Throws Exception

* /

Public void savefile () throws exception {

IF (filename.length () == 0) Throw new Exception

("You need to specify the saved configuration file name");

Savefile (filename);

}

/ **

* Delete an attribute

* @Param Value String

* /

Public void deleteValue (string value) {

P.Remove (Value);

}

/ **

* Main Method for Test

* @Param args string []

* /

Public static void main (String [] args) {

String file = "f: //p.properties";

PropertiesUTIL PU = New PropertiesUTIL (file);

pu.list ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-79331.html

New Post(0)