前言
在项目开发过程中,可能有一些配置需要写在配置文件中,当时自己也不是很清楚具体要怎么获取到配置文件的信息,下面的文章是本人整合网上资料并且经过实战项目所整合的方法,希望能帮助到各位!
一、建立配置文件
(1)在resources文件夹下新建一个xxxxxx.properties文件
(2)文件中的内容如下
#签名密匙
KEY=abcdefg123456789
#1.超级管理员账户名称(可执行退款撤销操作权限)
ADMIN_JURISDICTION=admin
提示:写入的方式就是键值对方式,到时候利用工具类,填入键就可以获取到另外一遍的值的内容
二、创建获取工具类
代码如下:
import com.example.whinterface.constant.PublicsConstant;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Enumeration;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
/**
* 读取Properties中的参数
*/
public class PropertiesReader {
private static Log log = LogFactory.getLog(PropertiesReader.class);
private static ResourceBundle resources = null;
private static ResourceBundle namedResources = null;
private static void getBundle() {
try {
resources = ResourceBundle.getBundle("WeChatConfig", Locale.getDefault());
} catch (MissingResourceException mre) {
log.error(mre);
}
}
private static void getNamedBundle(String filename) {
try {
namedResources = ResourceBundle.getBundle(filename, Locale.getDefault());
} catch (MissingResourceException mre) {
log.error(mre);
}
}
public static String getValue(String key, String filename) {
getNamedBundle(filename);
try {
return namedResources.getString(key);
} catch (Exception e) {
return null;
}
}
private static boolean checkResources() {
if (resources == null)
getBundle();
return (resources != null);
}
private static boolean changeToBoolean(String str) throws Exception {
String tmp = str.toLowerCase();
if (tmp.equals("true"))
return true;
else if (tmp.equals("false"))
return false;
else
throw new Exception("不能找到资源文件");
}
public static boolean getBoolean(String key) {
String str = getString(key);
try {
return changeToBoolean(str);
} catch (Exception e) {
return false;
}
}
public static boolean getBoolean(String key, boolean defaultValue) {
String str = getString(key);
try {
return changeToBoolean(str);
} catch (Exception e) {
return defaultValue;
}
}
private static int changeToInt(String str) throws Exception {
return Integer.parseInt(str);
}
public static int getInt(String key) {
String str = getString(key);
try {
return changeToInt(str);
} catch (Exception e) {
return 0;
}
}
public static int getInt(String key, int defaultValue) {
String str = getString(key);
try {
return changeToInt(str);
} catch (Exception e) {
return defaultValue;
}
}
public static String getString(String key, String defaultValue) {
String tmp = null;
if (checkResources()) {
try {
tmp = resources.getString(key);
} catch (Exception e) {
tmp = defaultValue;
}
}
return tmp;
}
public static String getString(String key) {
if (checkResources()) {
try {
return resources.getString(key);
} catch (Exception e) {
;
}
}
return null;
}
public static String[] getStringArray(String key) {
if (checkResources())
return resources.getStringArray(key);
return null;
}
public static Enumeration<String> getKeys() {
return resources.getKeys();
}
}
提示:工具类中的WeChatConfig就是配置类的名称,根据自己创建的实际情况进行修改
二、实际使用
(1)调用方法很简单,就一行代码
代码如下:
PropertiesReader.getString("KEY")
这样将结果获取到并且打印出来后就是配置文件中的:abcdefg123456789
总结
例如:以上就是今天要讲的内容,如果有什么不足的欢迎大家下方评论指导,谢谢合作!