properties 文件读取

假设在SRC根目录下有文件 config.properties ,下面代码就是读取这个文件的例子程序

方法1

Properties prop = new Properties();
InputStream in = Object.class.getResourceAsStream("/config.properties");

prop.load(in);
String databasePath = prop.getProperty("DATA_SOURCE").trim();


补充说明:要获取某包下的配置文件可以通过某类获取同一级包下的文件,具体为

//Object 就是指定的类,下面的方法会去指定类的包下找叫config.properties的配置文件
InputStream in = Object.class.getResourceAsStream("config.properties");



方法2

String fp = this.getClass().getClassLoader().getResource("/").getPath() + "config.properties";


File file = new File(fp);
Properties properties = new Properties();
properties.load(new FileInputStream(file));

String urlfilter = properties.getProperty("DATA_SOURCE");



写某包下的Properties文件

//MyObject是项目里面的类
String fp = MyObject.getClass().getClassLoader().getResource("/cn/myproject/").getPath() + "config.properties";


File file = new File(fp);
Properties properties = new Properties();
properties.load(new FileInputStream(file));

String urlfilter = properties.getProperty("DATA_SOURCE");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值