Tutorials to .com

Tutorials to .com » Jsp » Skills » java class in the use of Properties

java class in the use of Properties

Print View , by: iSee ,Total views: 19 ,Word Count: 885 ,Date: Sat, 22 Aug 2009 Time: 4:04 AM

package com.adrop.util;



import java.io. *;

import java.util.Properties;

import javax.servlet.http .*;

import javax.servlet .*;

import javax.servlet.jsp .*;



public class PropertiesUtil (

private String fileName;

private Properties p;

private FileInputStream in;

private FileOutputStream out;

/ **

* Based on the file name Chuanjin document

* @ Param fileName String

* /

public PropertiesUtil (String fileName) (

this.fileName = fileName;

File file = new File (fileName);

try (

in = new FileInputStream (file);

p = new Properties ();

/ / Load file

p.load (in);

in.close ();

)

catch (FileNotFoundException e) (

System.err.println ( "configuration file not found !!"); config.properties

e.printStackTrace ();

)

catch (Exception e) (

System.err.println ( "Error reading configuration file !!"); config.properties

e.printStackTrace ();

)

)



/ **

* Configuration files are for config.propertities, and reunification on the web application's root directory.

* @ Return String

* /

public static String getConfigFile (HttpServlet hs) (



return getConfigFile (hs, "config.properties");

)

/ **

* Used in the servlet, the direct use of this as a parameter, HttpServlet type

* The configuration file name from the current web application's root directory to find configuration file

* @ Param hs HttpServlet

* @ Param configFileName String configuration file 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 parameters used for pageContext

* @ Param hs PageContext

* @ Param configFileName String configuration file 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 the contents of the configuration file

* /

public void list () (

p.list (System.out);

)



/ **

* Name of the designated configuration item, to return to configuration values

* @ Param itemName String

* @ Return String

* /

public String getValue (String itemName) (

return p.getProperty (itemName);

)



/ **

* Configuration of the designated name and the default value, return to configuration values

* @ Param itemName String

* @ Param defaultValue String

* @ Return String

* /

public String getValue (String itemName,

String defaultValue) (

return p.getProperty (itemName, defaultValue);

)



/ **

* Set the name of configuration items and their value

* @ Param itemName String

* @ Param value String

* /

public void setValue (String itemName, String value) (

p.setProperty (itemName, value);

return;

)



/ **

* Save configuration file, specify the file name and description of the rise

* @ 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 the file

out.close ();

)

catch (IOException ex) (

throw new Exception

( "Can not save the configuration file specified:" + fileName);

)

)



/ **

* Save configuration file, specify the file name

* @ Param fileName String

* @ Throws Exception

* /

public void saveFile (String fileName)

throws Exception (

saveFile (fileName ,"");

)



/ **

* Save configuration file, using the original file name

* @ Throws Exception

* /

public void saveFile () throws Exception (

if (fileName.length () == 0)

throw new Exception

( "The need to preserve the configuration of the specified file name");

saveFile (fileName);

)

/ **

* Remove 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 ();

)

)



Java skills and code Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.