从 Properties 获取 int、float、boolean 和 string
Get int, float, boolean and string from Properties
我有属性文件中的整数、浮点数、布尔值和字符串。一切都已加载到属性中。目前,我正在解析值,因为我知道特定键的期望值。
Boolean.parseBoolean("false");
Integer.parseInt("3")
设置这些常量值的更好方法是什么,如果我不知道键的原始值数据类型是什么。
public class Messages {
Properties appProperties = null;
FileInputStream file = null;
public void initialization() throws Exception {
appProperties = new Properties();
try {
loadPropertiesFile();
} catch (Exception e) {
throw new Exception(e.getMessage(), e);
}
}
public void loadPropertiesFile() throws IOException {
String path = "./cfg/message.properties";
file = new FileInputStream(path);
appProperties.load(file);
file.close();
}
}
属性文件。
messassge.properties
SSO_URL = https://example.com/connect/token
SSO_API_USERNAME = test
SSO_API_PASSWORD = Uo88YmMpKUp
SSO_API_SCOPE = intraday_api
SSO_IS_PROXY_ENABLED = false
SSO_MAX_RETRY_COUNT = 3
SSO_FLOAT_VALUE = 3.0
Constant.java
public class Constants {
public static String SSO_URL = null;
public static String SSO_API_USERNAME = null;
public static String SSO_API_PASSWORD = null;
public static String SSO_API_SCOPE = null;
public static boolean SSO_IS_PROXY_ENABLED = false;
public static int SSO_MAX_RETRY_COUNT = 0;
public static float SSO_FLOAT_VALUE = 0;
}
如果你知道常量的类型,你可以使用Apache Commons Collections。
例如,您可以根据常量类型使用一些实用程序方法。
booelan SSO_IS_PROXY_ENABLED = MapUtils.getBooleanValue(appProperties, "SSO_IS_PROXY_ENABLED", false);
String SSO_URL = MapUtils.getString(appProperties, "SSO_URL", "https://example.com/connect/token");
您甚至可以使用默认值来避免错误。
Dambros 是对的,您在 Properties 文件中存储的所有内容都是字符串值。
您可以在检索属性值后跟踪不同的原始数据类型,如下所示,如 ref。 -
Java Properties File: How to Read config.properties Values in Java?
package crunchify.com.tutorial;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
/**
* @author Crunchify.com
*
*/
public class CrunchifyGetPropertyValues {
String result = "";
InputStream inputStream;
public String getPropValues() throws IOException {
try {
Properties prop = new Properties();
String propFileName = "config.properties";
inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
Date time = new Date(System.currentTimeMillis());
// get the property value and print it out
String user = prop.getProperty("user");
String company1 = prop.getProperty("company1");
String company2 = prop.getProperty("company2");
String company3 = prop.getProperty("company3");
result = "Company List = " + company1 + ", " + company2 + ", " + company3;
System.out.println(result + "\nProgram Ran on " + time + " by user=" + user);
} catch (Exception e) {
System.out.println("Exception: " + e);
} finally {
inputStream.close();
}
return result;
}
}
然后转换为原始 -
How to convert String to primitive type value?
我建议您通过将键值放在字符串类型 switch 语句中来跟踪您的数据类型值,然后使用键名大小写检索相关数据类型值。
Java 7.
之后可以使用字符串类型的 switch case
如果您有 class 个配置值,例如 Constants
class,并且您想要从配置(属性)文件加载所有值,您可以创建一个小帮手class并使用反射:
public class ConfigLoader {
public static void load(Class<?> configClass, String file) {
try {
Properties props = new Properties();
try (FileInputStream propStream = new FileInputStream(file)) {
props.load(propStream);
}
for (Field field : configClass.getDeclaredFields())
if (Modifier.isStatic(field.getModifiers()))
field.set(null, getValue(props, field.getName(), field.getType()));
} catch (Exception e) {
throw new RuntimeException("Error loading configuration: " + e, e);
}
}
private static Object getValue(Properties props, String name, Class<?> type) {
String value = props.getProperty(name);
if (value == null)
throw new IllegalArgumentException("Missing configuration value: " + name);
if (type == String.class)
return value;
if (type == boolean.class)
return Boolean.parseBoolean(value);
if (type == int.class)
return Integer.parseInt(value);
if (type == float.class)
return Float.parseFloat(value);
throw new IllegalArgumentException("Unknown configuration value type: " + type.getName());
}
}
然后你这样称呼它:
ConfigLoader.load(Constants.class, "/path/to/constants.properties");
您可以扩展代码以处理更多类型。您还可以更改它以忽略缺少的属性,而不是像现在这样失败,这样字段声明中的分配将保持不变,即默认。
不完全确定我是否完全理解问题,但有可能在 (String) 值中包含 属性 值的类型。因此,例如,您显示的属性将变为:
SSO_URL = URL:https://example.com/connect/token
SSO_API_USERNAME = STRING:test
SSO_API_PASSWORD = STRING:Uo88YmMpKUp
SSO_API_SCOPE = STRING:intraday_api
SSO_IS_PROXY_ENABLED = BOOLEAN:false
SSO_MAX_RETRY_COUNT = INTEGER:3
SSO_FLOAT_VALUE = FLOAT:3.0
在解析 属性 值期间,您首先通过查看 :
之前的部分来确定 属性 的类型,然后使用之后的部分进行实际解析。
private static Object getValue(Properties props, String name) {
String propertyValue = props.getProperty(name);
if (propertyValue == null) {
throw new IllegalArgumentException("Missing configuration value: " + name);
} else {
String[] parts = string.split(":");
switch(parts[0]) {
case "STRING":
return parts[1];
case "BOOLEAN":
return Boolean.parseBoolean(parts[1]);
....
default:
throw new IllegalArgumentException("Unknown configuration value type: " + parts[0]);
}
}
}
Spring 启动已准备就绪,可以使用并且功能达到 type-safe configuration 属性的解决方案。
当然,仅针对此任务使用 Spring 有点矫枉过正,但 Spring 有很多很酷的功能,这个功能可以吸引您到右侧 ;)
遵循 dropwizard 配置模式,使用 YAML 而不是 Properties 定义常量,并使用 Jackson 将其反序列化到 Class。除了类型安全之外,dropwizard 的配置模式更进一步,它允许 Hibernate Validator 注释来验证值是否在您的预期范围内。
以 dropwizard 为例...
有关所涉及技术的更多信息...
- github.com/FasterXML/jackson-dataformat-yaml
- 休眠。org/validator/
您可以在您选择的 class 中将可配置参数定义为 'static',并从静态 init 调用一个从属性文件加载参数值的方法。
例如:
public class MyAppConfig {
final static String propertiesPath="/home/workspace/MyApp/src/config.properties";
static String strParam;
static boolean boolParam;
static int intParam;
static double dblParam;
static {
// Other static initialization tasks...
loadParams();
}
private static void loadParams(){
Properties prop = new Properties();
try (InputStream propStream=new FileInputStream(propertiesPath)){
// Load parameters from config file
prop.load(propStream);
// Second param is default value in case key-pair is missing
strParam=prop.getProperty("StrParam", "foo");
boolParam=Boolean.parseBoolean(prop.getProperty("boolParam", "false"));
intParam= Integer.parseInt(prop.getProperty("intParam", "1"));
dblParam=Double.parseDouble(prop.getProperty("dblParam", "0.05"));
} catch (IOException e) {
logger.severe(e.getMessage());
e.printStackTrace();
}
}
}
这可能有帮助:
props.getProperty("name", Integer.class);
我有属性文件中的整数、浮点数、布尔值和字符串。一切都已加载到属性中。目前,我正在解析值,因为我知道特定键的期望值。
Boolean.parseBoolean("false");
Integer.parseInt("3")
设置这些常量值的更好方法是什么,如果我不知道键的原始值数据类型是什么。
public class Messages {
Properties appProperties = null;
FileInputStream file = null;
public void initialization() throws Exception {
appProperties = new Properties();
try {
loadPropertiesFile();
} catch (Exception e) {
throw new Exception(e.getMessage(), e);
}
}
public void loadPropertiesFile() throws IOException {
String path = "./cfg/message.properties";
file = new FileInputStream(path);
appProperties.load(file);
file.close();
}
}
属性文件。 messassge.properties
SSO_URL = https://example.com/connect/token
SSO_API_USERNAME = test
SSO_API_PASSWORD = Uo88YmMpKUp
SSO_API_SCOPE = intraday_api
SSO_IS_PROXY_ENABLED = false
SSO_MAX_RETRY_COUNT = 3
SSO_FLOAT_VALUE = 3.0
Constant.java
public class Constants {
public static String SSO_URL = null;
public static String SSO_API_USERNAME = null;
public static String SSO_API_PASSWORD = null;
public static String SSO_API_SCOPE = null;
public static boolean SSO_IS_PROXY_ENABLED = false;
public static int SSO_MAX_RETRY_COUNT = 0;
public static float SSO_FLOAT_VALUE = 0;
}
如果你知道常量的类型,你可以使用Apache Commons Collections。
例如,您可以根据常量类型使用一些实用程序方法。
booelan SSO_IS_PROXY_ENABLED = MapUtils.getBooleanValue(appProperties, "SSO_IS_PROXY_ENABLED", false);
String SSO_URL = MapUtils.getString(appProperties, "SSO_URL", "https://example.com/connect/token");
您甚至可以使用默认值来避免错误。
Dambros 是对的,您在 Properties 文件中存储的所有内容都是字符串值。 您可以在检索属性值后跟踪不同的原始数据类型,如下所示,如 ref。 -
Java Properties File: How to Read config.properties Values in Java?
package crunchify.com.tutorial;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
/**
* @author Crunchify.com
*
*/
public class CrunchifyGetPropertyValues {
String result = "";
InputStream inputStream;
public String getPropValues() throws IOException {
try {
Properties prop = new Properties();
String propFileName = "config.properties";
inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
Date time = new Date(System.currentTimeMillis());
// get the property value and print it out
String user = prop.getProperty("user");
String company1 = prop.getProperty("company1");
String company2 = prop.getProperty("company2");
String company3 = prop.getProperty("company3");
result = "Company List = " + company1 + ", " + company2 + ", " + company3;
System.out.println(result + "\nProgram Ran on " + time + " by user=" + user);
} catch (Exception e) {
System.out.println("Exception: " + e);
} finally {
inputStream.close();
}
return result;
}
}
然后转换为原始 - How to convert String to primitive type value?
我建议您通过将键值放在字符串类型 switch 语句中来跟踪您的数据类型值,然后使用键名大小写检索相关数据类型值。 Java 7.
之后可以使用字符串类型的 switch case如果您有 class 个配置值,例如 Constants
class,并且您想要从配置(属性)文件加载所有值,您可以创建一个小帮手class并使用反射:
public class ConfigLoader {
public static void load(Class<?> configClass, String file) {
try {
Properties props = new Properties();
try (FileInputStream propStream = new FileInputStream(file)) {
props.load(propStream);
}
for (Field field : configClass.getDeclaredFields())
if (Modifier.isStatic(field.getModifiers()))
field.set(null, getValue(props, field.getName(), field.getType()));
} catch (Exception e) {
throw new RuntimeException("Error loading configuration: " + e, e);
}
}
private static Object getValue(Properties props, String name, Class<?> type) {
String value = props.getProperty(name);
if (value == null)
throw new IllegalArgumentException("Missing configuration value: " + name);
if (type == String.class)
return value;
if (type == boolean.class)
return Boolean.parseBoolean(value);
if (type == int.class)
return Integer.parseInt(value);
if (type == float.class)
return Float.parseFloat(value);
throw new IllegalArgumentException("Unknown configuration value type: " + type.getName());
}
}
然后你这样称呼它:
ConfigLoader.load(Constants.class, "/path/to/constants.properties");
您可以扩展代码以处理更多类型。您还可以更改它以忽略缺少的属性,而不是像现在这样失败,这样字段声明中的分配将保持不变,即默认。
不完全确定我是否完全理解问题,但有可能在 (String) 值中包含 属性 值的类型。因此,例如,您显示的属性将变为:
SSO_URL = URL:https://example.com/connect/token
SSO_API_USERNAME = STRING:test
SSO_API_PASSWORD = STRING:Uo88YmMpKUp
SSO_API_SCOPE = STRING:intraday_api
SSO_IS_PROXY_ENABLED = BOOLEAN:false
SSO_MAX_RETRY_COUNT = INTEGER:3
SSO_FLOAT_VALUE = FLOAT:3.0
在解析 属性 值期间,您首先通过查看 :
之前的部分来确定 属性 的类型,然后使用之后的部分进行实际解析。
private static Object getValue(Properties props, String name) {
String propertyValue = props.getProperty(name);
if (propertyValue == null) {
throw new IllegalArgumentException("Missing configuration value: " + name);
} else {
String[] parts = string.split(":");
switch(parts[0]) {
case "STRING":
return parts[1];
case "BOOLEAN":
return Boolean.parseBoolean(parts[1]);
....
default:
throw new IllegalArgumentException("Unknown configuration value type: " + parts[0]);
}
}
}
Spring 启动已准备就绪,可以使用并且功能达到 type-safe configuration 属性的解决方案。
当然,仅针对此任务使用 Spring 有点矫枉过正,但 Spring 有很多很酷的功能,这个功能可以吸引您到右侧 ;)
遵循 dropwizard 配置模式,使用 YAML 而不是 Properties 定义常量,并使用 Jackson 将其反序列化到 Class。除了类型安全之外,dropwizard 的配置模式更进一步,它允许 Hibernate Validator 注释来验证值是否在您的预期范围内。
以 dropwizard 为例...
有关所涉及技术的更多信息...
- github.com/FasterXML/jackson-dataformat-yaml
- 休眠。org/validator/
您可以在您选择的 class 中将可配置参数定义为 'static',并从静态 init 调用一个从属性文件加载参数值的方法。
例如:
public class MyAppConfig {
final static String propertiesPath="/home/workspace/MyApp/src/config.properties";
static String strParam;
static boolean boolParam;
static int intParam;
static double dblParam;
static {
// Other static initialization tasks...
loadParams();
}
private static void loadParams(){
Properties prop = new Properties();
try (InputStream propStream=new FileInputStream(propertiesPath)){
// Load parameters from config file
prop.load(propStream);
// Second param is default value in case key-pair is missing
strParam=prop.getProperty("StrParam", "foo");
boolParam=Boolean.parseBoolean(prop.getProperty("boolParam", "false"));
intParam= Integer.parseInt(prop.getProperty("intParam", "1"));
dblParam=Double.parseDouble(prop.getProperty("dblParam", "0.05"));
} catch (IOException e) {
logger.severe(e.getMessage());
e.printStackTrace();
}
}
}
这可能有帮助:
props.getProperty("name", Integer.class);