📄 config.java
字号:
//-------------------------------------------------------------------------
// Copyright (c) 2000-2007 Ufinity. All Rights Reserved.
//
// This software is the confidential and proprietary information of
// Ufinity
//-------------------------------------------------------------------------
// UFINITY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
// THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. GEMPLUS SHALL NOT BE
// LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
// MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
//
// THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
// CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
// PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
// NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
// SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
// SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
// PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). GEMPLUS
// SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
// HIGH RISK ACTIVITIES.
//-------------------------------------------------------------------------
package org.xk.util;
import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
/**
* This class simplifies reading configuration files.
*
* @author xzr
*/
public class Config {
private static Properties _prop;
private static String FILE_LOCATION = "/config.properties";
/**
* Initializes the config.
*
* @param confFile
* the config file path it will search for the file in the class
* path if the path doesn't exist
* @exception InstantiationException
*/
static {
try {
InputStream is;
File file = new File(FILE_LOCATION);
if (file.exists()) {
is = new FileInputStream(FILE_LOCATION);
} else {
System.out.println("input conf file path not exists:"
+ FILE_LOCATION + ", trying to search from class path.");
is = Config.class.getClassLoader().getResourceAsStream(
FILE_LOCATION.substring(FILE_LOCATION.lastIndexOf("/")+1));
}
_prop = new Properties();
_prop.load(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
/**
* Read a string value
*
* @param key
* key from config file
* @return String value if exists
*/
public static String getString(String key) {
if (_prop == null) {
throw new RuntimeException("Config hasn't been initialized yet.");
}
if (_prop.containsKey(key))
return _prop.getProperty(key);
else
throw new RuntimeException("No config key found for [" + key + "]."
+ " from prop:" + _prop);
}
/**
* Read a int value
*
* @param key
* key from config file
* @return int value if exists
*/
public static int getInt(String key) {
if (_prop == null) {
throw new RuntimeException("Config hasn't been initialized yet.");
}
if (_prop.containsKey(key))
return Integer.parseInt(_prop.getProperty(key));
else
throw new RuntimeException("No config key found for [" + key + "].");
}
/**
* Read a long value
*
* @param key
* key from config file
* @return long value if exists
*/
public static long getLong(String key) {
if (_prop == null) {
throw new RuntimeException("Config hasn't been initialized yet.");
}
if (_prop.containsKey(key))
return Long.parseLong(_prop.getProperty(key));
else
throw new RuntimeException("No config key found for [" + key + "].");
}
/**
* Read a boolean value
*
* @param key
* key from config file
* @return boolean true if value is 1 or true
*/
public static boolean getBoolean(String key) {
if (_prop == null) {
throw new RuntimeException("Config hasn't been initialized yet.");
}
if (_prop.containsKey(key))
if (_prop.getProperty(key).equals("true")
|| _prop.getProperty(key).equals("1"))
return true;
else
return false;
else
throw new RuntimeException("No config key found for [" + key + "].");
}
/**
* Read a byte value
*
* @param key
* key from config file
* @return byte value if exists
*/
public static byte getByte(String key) {
if (_prop == null) {
throw new RuntimeException("Config hasn't been initialized yet.");
}
if (_prop.containsKey(key))
return Byte.parseByte(_prop.getProperty(key));
else
throw new RuntimeException("No config key found for [" + key + "].");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -