
java代码中读取properties文件
发布日期:2022-02-10 11:36:52
浏览次数:20
分类:技术文章
本文共 1646 字,大约阅读时间需要 5 分钟。
一般情况下,我们都会采用spring xml + propeties文件的形式对java代码中的某些属性进行配置。但在一些比较小的程序中,其实根本用不到
Spring框架,不需要依赖注入。此时,我们可以直接应用properties文件,具体配置方法如下:
注意:下面的pro.properties文件只需要被放在BuildPath下面即可。
public class ProConfig {
private static Logger logger = Logger.getLogger(ProConfig.class); private Properties properties = new Properties(); public static final ProConfig Default = new ProConfig("/pro.properties"); public ProConfig(String configPath) { InputStream is = null; try { is = ProConfig.class.getResourceAsStream(configPath); properties.load(is); } catch (IOException e) { logger.error("Failed to load config file at: " + configPath, e); } finally { if (is == null) { return; } try { is.close(); } catch (IOException e) { logger.error( "Failed to close input stream when loading config file at: " + configPath, e); } } } public Properties getProperties() { return properties; } public String getProperty(String name) { return properties.getProperty(name); } public int getInt(String name) { return getInt(name, 0); } public int getInt(String name, int defVal) { int ret = defVal; try { ret = Integer.parseInt(properties.getProperty(name)); } catch (Exception e) { } return ret; } public long getLong(String name) { return getLong(name, 0L); } public long getLong(String name, long defVal) { long ret = defVal; try { ret = Long.parseLong(properties.getProperty(name)); } catch (Exception e) { } return ret; } public boolean getBoolean(String name) { return getBoolean(name, false); } public boolean getBoolean(String name, boolean defVal) { boolean ret = defVal; try { ret = Boolean.parseBoolean(properties.getProperty(name)); } catch (Exception e) { } return ret; }转载地址:https://blog.csdn.net/courage89/article/details/8969085 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2023年05月31日 09时40分59秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
最新文章
电子书下载:Microsoft Visual C# 2010 Step by Step
2019-07-09 05:52:45
Asp.Net MVC 4 Web API 中的安全认证-使用OAuth
2019-07-09 05:52:44
如何关注那些有价值的微博
2019-07-09 05:52:44
ASP.NET 使用PageBase来控制用户登陆
2019-07-09 05:52:43
(筆記) fmax的計算公式 (SOC) (Quartus II)
2019-07-09 05:52:43
负载均衡下 tomcat session 共享
2019-07-09 05:52:42
mysql 比较函数和操作符
2019-07-09 05:52:42
进程管理利器Supervisor--入门简介
2019-07-09 05:52:42
工控随笔_15_西门子_WinCC的VBS脚本_05_变量类型之三_VBS数组
2019-07-09 05:52:41
Idea开启Run DashBoard配置
2019-07-09 05:52:41
linux 监控性能学习笔记(1)
2019-07-09 05:52:40
180418
2019-07-09 05:52:40
Silverlight & Blend动画设计系列一:偏移动画(TranslateTransform)
2019-07-09 05:52:39
外卖O2O的用户画像实践
2019-07-09 05:52:39
HttpWebRequest使用注意(发生阻塞的解决办法)
2019-07-09 05:52:38
dom4j解析xml字符串实例
2019-07-09 05:52:38
.NET微服务调查结果
2019-07-09 05:52:37
爬虫开发python工具包介绍 (1)
2019-07-09 05:52:37
python对象池模式
2019-07-09 05:52:36
vagrant up 时提示错误 cound not open file
2019-07-09 05:52:36