目录
前言
在工作中为了方便项目管理,通常会用到配置文件,以前用的都是配置excel表格转成json格式文件,再读取数据,记录一些有用的方法,也提供给大家参考
Java读取properties配置文件
这种解析方式就轻便很多,适用于配置文件数据小的场景
配置文件数据
读取文件方式
@Override
protected void register() {
Properties p = PropertiesUtils.read("platform/huaweih5.properties");
APP_ID = PropertiesUtils.getString(p, "appid");
USER_ID = PropertiesUtils.getString(p, "userID");
PAY_PUBLIC_KEY = PropertiesUtils.getString(p, "payPublicKey");
PAY_PRIVATE_KEY = PropertiesUtils.getString(p, "payPrivateKey");
PUBLIC_KEY = PropertiesUtils.getString(p, "publicKey");
context.put(CHANNEL_NAME, this);
}
解析方式
/**
* Properties帮助类
* @author CharonWang
*
*/
public class PropertiesUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesUtils.class);
/**
* 读取properties文件
* @param filePath
* @return
*/
public static Properties read(String filePath) {
Properties properties = new Properties();
InputStream is = PropertiesUtils.class.getClassLoader().getResourceAsStream(filePath);
if (is == null) {
LOGGER.error(String.format("[%s] file path not found.", filePath));
return null;
}
try {
properties.load(is);
is.close();
} catch (IOException e) {
LOGGER.error(String.format("[%s] file load error.", filePath));
return null;
} finally {
}
return properties;
}
/**
* 获取整型值
* @param p
* @param name
* @return
*/
public static int getInt(Properties p, String name) {
String str = p.getProperty(name).trim();
if (str == null || str.isEmpty()) {
return 0;
}
return Integer.valueOf(p.getProperty(name).trim());
}
/**
* @param p
* @param name
* @return
*/
public static long getLong(Properties p, String name) {
String str = p.getProperty(name).trim();
if (str == null || str.isEmpty()) {
return 0;
}
return Long.valueOf(p.getProperty(name).trim());
}
/**
* 获取字符串值
* @param p
* @param name
* @return
*/
public static String getString(Properties p, String name) {
return p.getProperty(name).trim();
}
/**
* 逗号分隔获取字符串List
* @param p
* @param name
* @return
*/
public static List<String> dotSplitStringList(Properties p, String name) {
return getSplitList(p, name, ",");
}
/**
* 逗号分隔获取整型List
* @param p
* @param name
* @return
*/
public static List<Integer> dotSplitIntList(Properties p, String name) {
return getSplitList(p, name, ",");
}
public static List<Long> dotSplitLongList(Properties p, String name) {
return getSplitList(p, name, ",");
}
@SuppressWarnings("unchecked")
public static <T> List<T> getSplitList(Properties p, String name, String splitChar) {
if (name == null || name.isEmpty()) {
return new ArrayList<>();
}
String splitString = getString(p, name);
if (splitString == null || splitString.isEmpty()) {
return new ArrayList<>();
}
String[] splitArray = splitString.split(splitChar.trim());
List<T> list = new ArrayList<>();
for (String s : splitArray) {
if (StringUtils.isNotBlank(s)) {
list.add((T) s.trim());
}
}
return list;
}
}
Lua读取config配置文件
先是excel表格转config文件,再读取数据
配置文件数据
配置文件的读取
实例中通过主键id读取数据
-- 领取奖励
function Achievement:GetReward(achievementid)
local achievementconfig = server.configCenter.AchievementConfig[achievementid]
if achievementconfig then
if achievementconfig.drop then
local rewards = server.dropCenter:DropGroup(achievementconfig.drop)
if rewards then
self.player:GiveItem(rewards, 1, 1,server.baseConfig.YuanbaoRecordType.Achievement)
end
end
end
if achievementconfig.title then
self.role.titleeffect:ActivatePart({id = achievementconfig.title,time = 0})
end
end
读取文件
将配置信息放入server.configCenter中,元表的骚操作是真的多,
function config:ShareConfig()
server.configCenter = {}
local mt = {}
mt.__index = function(_,key)
local cfg = lua_share.query(key)
if cfg == nil then
lua_app.log_error("config query failed",key)
return {}
end
return cfg
end
mt.__newindex = function(_,key,value)
lua_app.log_info("error config new")
end
setmetatable(server.configCenter,mt)
end
解析文件
lua_share方法
local function service()
if handler == 0 then
handler = lua_app.unique_lua("share/share_svc")
end
return handler
end
function sharedata.query(name,call)
if cache[name] then
return cache[name]
end
local obj = lua_app.call(service(), "lua", "query", name)
local r = sd.box(obj)
lua_app.send(service(), "lua", "confirm" , obj)
lua_app.fork(monitor,name, r, obj,call)
cache[name] = r
return r
end
lua_app方法
function unique_lua(name,...)
local handle = clib.unpack(raw_call(".launcher","lua",clib.pack("unique_process","LuaProxy",name,...)))
if type(handle) == "number" then
return handle
end
return 0
end
function call(addr,type_name,...)
local protocol = protocols[type_name]
if watching_process[addr] == false then
error("Service is dead")
end
local session = clib.new_session()
local ret = clib.send(addr,session,protocol.id,protocol.pack(...))
if ret == nil or ret == -1 then
error("call to invalid address "..tostring(addr))
end
return protocol.unpack(yield_call(addr,session))
end
function send(dest,type_name,...)
local protocol = protocols[type_name]
if watching_process[dest] == false then
error("Service is dead")
end
return clib.send(dest,0,protocols[type_name].id,protocol.pack(...))
end
function fork(func,...)
local args = {...}
local co = new_co(function()
func(table.unpack(args))
end)
table.insert(fork_queue,co)
return co
end
引用的clib应该是C++文件
share_core方法
function conf.box(obj)
local gcobj = core.box(obj)
return setmetatable({
__parent = false,
__obj = obj,
__gcobj = gcobj,
__key = "",
} , meta)
end
Java读取json配置文件
先是excel表格转json文件,再读取数据,每次需要新建一个对应配置类,功能实现比较复杂,节省篇幅删减部分字段
配置文件数据
fileName对应配置文件名称,新建配置类对应文件字段
@DataFile(fileName = "seize_mine_config")
public class SeizeMineConfig implements ModelAdapter {
//矿ID
private int id;
// 占领箱子
private String boxReward;
//消耗
private String seizeConsume;
@FieldIgnore
private List<CountProduct> seizeConsumes;
@Override
public void initialize() {
//字符串转物品类 id count
seizeConsumes = CountProduct.create(seizeConsume);
}
@Override
public IdentiyKey findKey() {
return IdentiyKey.build(id);
}
public int getId()