⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propertiesmanager.java

📁 java 写的一个新闻发布系统
💻 JAVA
字号:
// $Id: PropertiesManager.java,v 1.3 2002/06/04 15:07:49 knguyen Exp $////                                   ____.//                       __/\ ______|    |__/\.     _______//            __   .____|    |       \   |    +----+       \//    _______|  /--|    |    |    -   \  _    |    :    -   \_________//   \\______: :---|    :    :           |    :    |         \________>//           |__\---\_____________:______:    :____|____:_____\//                                      /_____|////              . . . i n   j a h i a   w e   t r u s t . . .//////  PropertiesManager////  27.03.2001  AK  added in jahia.//  28.03.2001  AK  mammouth fix into method storeProperties.//  12.08.2001  NK  Close input stream after reading from property file, otherwhise the file cannot be deleted.//package org.jahia.utils.properties;import java.io.*;import java.util.*;import org.jahia.utils.*;/** * desc:  This class provides to you a *super interface* for properties. * It allows you to create a new properties file, set properties, remove * properties, get properties, get properties from an existing properties * object or from a flat file, get a complete properties object, and you can * store the new properties object where you want on the filesystem. The store * method keep your base properties file design (not like the store() method * from java properties object)! * * Copyright:    Copyright (c) 2002 * Company:      Jahia Ltd * * @author Alexandre Kraft * @version 1.0 */public class PropertiesManager{    public Properties properties =  new Properties();    public String     propertiesFilePath;    /**	 * Default constructor.	 * @author Alexandre Kraft	 */    public PropertiesManager()    {        // do nothing :o)    } // end constructor    /**	 * Default constructor.	 * @author Alexandre Kraft	 *	 * @param   propertiesFilePath  The filesystem path from the file is loaded.	 */    public PropertiesManager( String propertiesFilePath )    {        this.propertiesFilePath =  propertiesFilePath;        this.loadProperties();    } // end constructor    /**	 * Default constructor.	 * @author Alexandre Kraft	 *	 * @param   properties  The properties object used to define base properties.	 */    public PropertiesManager( Properties properties )    {        this.properties =  properties;    } // end constructor    /**	 * Load a complete properties file in memory by its filename.	 * @author Alexandre Kraft	 */    private void loadProperties()    {    	FileInputStream inputStream = null;        try        {            inputStream =  new FileInputStream( propertiesFilePath );            properties  = new Properties();            properties.load( inputStream );        } catch (IOException ioe) {            JahiaConsole.println( "PropertiesManager", "IOException on loadProperties()." );        } catch (SecurityException se) {            JahiaConsole.println( "PropertiesManager", "SecurityException on file ["+propertiesFilePath+"]");        } finally {        	try {	        	inputStream.close();    	    	inputStream = null;    	    } catch (Throwable t){    	    	t.printStackTrace();    	    }        }    } // end loadProperties    /**	 * Get a property value by its name.	 * @author Alexandre Kraft     *	 * @param   propertyName    The property name to get its value.	 * @return  Returns a String containing the value of the property name.	 */    public String getProperty( String propertyName )    {        return properties.getProperty( propertyName );    } // end getProperty    /**	 * Set a property value by its name.	 * @author Alexandre Kraft     *	 * @param   propertyName    The property name to set.	 * @param   propvalue   The property value to set.	 */    public void setProperty( String propertyName,                             String propvalue )    {        properties.setProperty( propertyName, propvalue );    } // end setProperty    /**	 * Remove a property by its name.	 * @author Alexandre Kraft     *	 * @param   propertyName    The property name to remove.	 */    public void removeProperty( String propertyName )    {        properties.remove( propertyName );    } // end removeProperty	/**	 * Store new properties and values in the properties file.	 * The file writed is the same, using the same file path, as the file loaded before.	 * @author Alexandre Kraft	 */    public void storeProperties()    {        try {            storeProperties( this.propertiesFilePath );        } catch (NullPointerException npe) {            JahiaConsole.println( "PropertiesManager", "NullPointerException on storeProperties()." );        }    } // end storeProperties    /**	 * Store new properties and values in the properties file.	 * If the file where you want to write doesn't exists, the file is created.	 * @author  Alexandre Kraft	 * @author  Khue N'Guyen	 *	 * @param   propertiesFilePath  The filesystem path where the file is saved.	 */    public void storeProperties( String propertiesFilePath )    throws NullPointerException    {        boolean     baseObjectExists         =  true;        Vector      bufferVector             =  new Vector();        String      lineReaded               =  null;        File        propertiesFileObject     =  new File( propertiesFilePath );        File        propertiesFileFolder     =  propertiesFileObject.getParentFile();        // check if the destination folder exists and create it if needed...        if(!propertiesFileFolder.exists()) {            propertiesFileFolder.mkdirs();            propertiesFileFolder = null;        }        // try to create a file object via the propertiesFilePath...        File propertiesFileObjectBase = null;        try {            propertiesFileObjectBase    =  new File( this.propertiesFilePath );                        } catch (NullPointerException npe) {            baseObjectExists = false;        } finally {        	propertiesFileObjectBase = null;        }        try        {            if(baseObjectExists)            {                BufferedReader buffered                  =  new BufferedReader( new FileReader( this.propertiesFilePath ) );                int            position                  =  0;                // compose all properties vector, used to find the new properties...                Vector         allProperties             =  new Vector();                Enumeration    allPropertiesEnumeration  =  properties.propertyNames();                while(allPropertiesEnumeration.hasMoreElements()) {                    allProperties.add( (String) allPropertiesEnumeration.nextElement() );                }                // parse the file...                while((lineReaded = buffered.readLine()) != null)                {                    try {                        if(!lineReaded.trim().equals("") && !lineReaded.trim().substring(0,1).equals("#"))                        {                            boolean     propertyFound =  false;                            int         countThisLine =  0;                            Enumeration propertyNames =  allProperties.elements();                            while(propertyNames.hasMoreElements() && !propertyFound)                            {                                String  propertyName  =  (String) propertyNames.nextElement();                                String  propvalue =  properties.getProperty( propertyName );                                if(lineReaded.indexOf(propertyName + " ") == 0) {                                    position =  lineReaded.indexOf("=");                                    if(position >= 0)                                    {                                        propertyFound  = true;                                        StringBuffer thisLineBuffer =  new StringBuffer();                                        thisLineBuffer.append( lineReaded.substring(0,position+1) );                                        thisLineBuffer.append( "   " );                                        thisLineBuffer.append( JahiaTools.string2Property( propvalue ) );                                        bufferVector.add( thisLineBuffer.toString() );                                        // remove this line from allProperties to affine the search and to find new properties...                                        allProperties.remove( propertyName );                                    }                                }                            }                        }                        else                        {                            // this is a special line only for layout, like a comment or a blank line...                            bufferVector.add( lineReaded.trim() );                        }                    } catch (IndexOutOfBoundsException ioobe) {                    }                }                // add not found properties at the end of the file (and the jahia.properties layout is keeping)...                Enumeration restantPropertyNames =  allProperties.elements();                while(restantPropertyNames.hasMoreElements())                {                    String restantPropertyName = (String) restantPropertyNames.nextElement();                    StringBuffer specialLineBuffer =  new StringBuffer();                    specialLineBuffer.append( restantPropertyName );                    for(int i=0; i<55-restantPropertyName.length(); i++) {                        specialLineBuffer.append( " " );                    }                    specialLineBuffer.append( "=   " );                    specialLineBuffer.append( properties.getProperty( restantPropertyName ) );                    bufferVector.add( specialLineBuffer.toString() );                }                // close the buffered filereader...                buffered.close();                // write the file...                writeTheFile( propertiesFilePath, bufferVector );            }            else            {                FileOutputStream outputStream =  new FileOutputStream( propertiesFileObject );                properties.store( outputStream, "This file has been written by Jahia." );                outputStream.close();            }        } catch (java.io.IOException ioe) {        }    } // end storeProperties    /**	 * Write the file composed by the storeProperties() method, using	 * @author Alexandre Kraft	 *	 * @param   propertiesFilePath  The filesystem path where the file is saved.	 * @param   bufferVector        Vector containing all the string lines of the new file.	 */    private void writeTheFile( String propertiesFilePath,                               Vector bufferVector )    {        File         thisFile     =  null;        FileWriter   fileWriter   =  null;        StringBuffer outputBuffer =  null;        try        {            thisFile     =  new File( propertiesFilePath );            fileWriter   =  new FileWriter( thisFile );            outputBuffer =  new StringBuffer();            for(int i=0; i < bufferVector.size(); i++) {                outputBuffer.append((String) bufferVector.get(i));                outputBuffer.append("\n");            }            fileWriter.write( outputBuffer.toString() );        } catch (java.io.IOException ioe) {    	} finally {            try {            	fileWriter.close();            }catch ( java.io.IOException ioe2 ){			}			fileWriter = null;			thisFile = null;    	    	}    } // end writeTheFile    /**	 * Get the properties object for the instance of this class.	 * @author Alexandre Kraft	 *	 * @return  The properties object for the instance of this class.	 */    public Properties getPropertiesObject()    {        return properties;    } // end getPropertiesObject} // end PropertiesManager

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -