Chinaunix

标题: 一个操作Properties的实用类。。 [打印本页]

作者: chenyajun5    时间: 2005-02-19 09:25
标题: 一个操作Properties的实用类。。
一个操作Properties的实用类。。
import java.io.*;
import java.net.*;
import java.util.*;
public class PropMan   
{
    private File propertiesFile;
    private static Map propetyManager = new HashMap();
    private static Object managerLock = new Object();
    private PropMan(String filename)
    {
        propertiesFile = null;
        setPropertyFile(filename);
    }
    public static PropMan getInstance(String propertyFileName)
    {
        return new PropMan(propertyFileName);
    }
    public void setPropertyFile(String filename)
    {
        try
        {
            propertiesFile = new File((new URI(getClass().getResource(filename).getPath())).getPath());
        }
        catch(URISyntaxException e)
        {
            System.out.println("error");
        }
    }
    private void loadProps(Properties prop)        throws UtilException
    {
        if(prop != null)
            prop.clear();
        InputStream in = null;
        try
        {
            in = new FileInputStream(propertiesFile);
            prop.load(in);
        }
        catch(Exception e)
        {
            String error = "Error reading the properties file: " + propertiesFile + " in PropMan.loadProps()n" + e;
            throw new UtilException(error);
        }
        finally
        {
            try
            {
                in.close();
            }
            catch(Exception e) { }
        }
        return;
    }
    private Properties getProperties()        throws UtilException
    {
        Properties prop = (Properties)propetyManager.get(propertiesFile);
        if(prop == null)
            synchronized(managerLock)
            {
                if(prop == null)
                {
                    prop = new Properties();
                    loadProps(prop);
                }
            }
        return prop;
    }
    public String getProperty(String name)
    {
        return getProperty(name, null);
    }
    public String getProperty(String name, String defaultString)
    {
        try
        {
            return getProperties().getProperty(name);
        }
        catch(Exception e)
        {
        }
        return defaultString;
    }
    public void setProperty(String name, String value)
    {
        Properties prop = null;
        try
        {
            prop = getProperties();
            if(prop == null)
                prop = new Properties();
        }
        catch(Exception e)
        {
            prop = new Properties();
        }
        prop.setProperty(name, value);
        propetyManager.put(propertiesFile, prop);
    }
    public void deleteProperty(String name)
    {
        try
        {
            Properties prop = getProperties();
            prop.remove(name);
            propetyManager.put(propertiesFile, prop);
        }
        catch(Exception e)
        {
        }
    }
    public void saveProperty()         throws UtilException
    {
        OutputStream out = null;
        try
        {
            if(!propertiesFile.exists())
                propertiesFile.createNewFile();
            out = new FileOutputStream(propertiesFile);
            ((Properties)propetyManager.get(propertiesFile)).store(out, propertiesFile.getPath());
        }
        catch(Exception ioe)
        {
            String errorMessage = "There was an error writing properties to " + propertiesFile + ". " ;
            throw new UtilException(errorMessage, ioe);
        }
        finally
        {
            try
            {
                out.close();
            }
            catch(Exception e) { }
        }
        return;
    }
    public void putProperty(String name, String value)
    {
        Properties prop = null;
        try
        {
            prop = getProperties();
            if(prop == null)
                prop = new Properties();
        }
        catch(Exception e)
        {
            prop = new Properties();
        }
        prop.put(name, value);
        propetyManager.put(propertiesFile, prop);
    }
    public Enumeration propertyNames()
    {
        try
        {
            return getProperties().propertyNames();
        }
        catch(UtilException e)
        {
            return ((Properties)propetyManager.get(propertiesFile)).propertyNames();
        }
    }
    public boolean propertyFileIsReadable()
    {
        try
        {
            return propertiesFile.canRead();
        }
        catch(Exception e)
        {
            return false;
        }
    }
    public boolean propertyFileIsWritable()
    {
        return propertiesFile.isFile() && propertiesFile.canWrite();
    }
    public boolean propertyFileExists()
    {
        return propertiesFile.exists() && propertiesFile.isFile();
    }
    public void reloadProps()         throws UtilException
    {
        loadProps(getProperties());
    }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/303/showart_12559.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2