main.java.com.aosa.util.AOSAReadProperties.java Source code

Java tutorial

Introduction

Here is the source code for main.java.com.aosa.util.AOSAReadProperties.java

Source

/**
 * AOSAReadProperties.java ,By AOSA_TECH DevicePlus_dg at 2012-1-12 ?05:46:45
 * Copyright (c) 2011, AOSA_TECH, Ltd. All right reserved.
 * AOSA_TECH PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * @Project DevicePlus_dg
 * @version V1.0.1
 */
package main.java.com.aosa.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * TitleAOSAReadProperties      <br>
 * Description<code>Properties?</code>      <br>
 * @author mutou at 2012-1-12 ?05:46:50   <br>
 * @version V1.0.1      <br>
 * @see
 */
public class AOSAReadProperties {
    static Log logger = LogFactory.getLog(AOSAReadProperties.class);

    /**
     * Description<code>?Properties</code>      <br>
     * By mutou at 2012-1-12 ?05:48:25   <br>
     * String      <br>
     * @param profileName
     *       Properties ??ClassPath
     * @param key
     *       ?? 
     * @return
     * @throws
     */
    public static String ReadValue(String profileName, String key) {
        String value = null;
        Properties properties = new Properties();
        FileInputStream input = null;
        try {
            input = new FileInputStream(
                    Thread.currentThread().getContextClassLoader().getResource("").getPath() + profileName);
            properties.load(input);
            value = properties.getProperty(key);
        } catch (FileNotFoundException e) {
            logger.debug("Properties");
            throw new AOSARuntimeException("Properties", e);
        } catch (IOException e) {
            logger.debug("PropertiesIO");
            throw new AOSARuntimeException("PropertiesIO", e);
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    logger.debug("IO?");
                    throw new AOSARuntimeException("IO?", e);
                }
            }
        }
        return value;
    }

    /**
     * Description<code>Properties</code>      <br>
     * By mutou at 2012-1-12 ?05:58:16   <br>
     * boolean      <br>
     * @param profileName
     *       Properties ??ClassPath
     * @param key
     *       ?? 
     * @param value
     *       ? value
     * @return
     * @throws
     */
    public static boolean WriteValue(String profileName, String key, String value) {
        boolean isWrite = false;
        Properties properties = new Properties();
        InputStream input = AOSAReadProperties.class.getClassLoader().getResourceAsStream(profileName);
        try {
            properties.load(input);
            properties.setProperty(key, value);
            URL prourl = AOSAReadProperties.class.getClassLoader().getResource(profileName);
            String filepath = prourl.getFile();
            FileOutputStream fileOutputStream = new FileOutputStream(filepath);
            properties.store(fileOutputStream, "Custum shutDownTime config : conf.properties");
            fileOutputStream.flush();
            fileOutputStream.close();
            isWrite = true;
        } catch (FileNotFoundException e) {
            logger.debug("Properties");
            throw new AOSARuntimeException("Properties", e);
        } catch (IOException e) {
            logger.debug("PropertiesIO");
            throw new AOSARuntimeException("PropertiesIO", e);
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    logger.debug("IO?");
                    throw new AOSARuntimeException("IO?", e);
                }
            }
        }
        return isWrite;
    }
}