如何读取项目工程下的properties文件

读取classess目录下的文件

代码实现

/**
	 * 根据资源名称获取Properties对象
	 * @param propertiesName
	 * @return
	 */
	protected static Properties getProperties(String propertiesName) {
		String path =  SystemPublicResourcesController.class.getProtectionDomain().getCodeSource().getLocation().getPath() + propertiesName;
		File file = new File(path);
		Properties prop = new Properties();
		InputStream in = null;
		try {
			 in = new BufferedInputStream(new FileInputStream(file));
			prop.load(in);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}finally {
			if(in != null) {
				try {
					in.close();
				} catch (IOException ex) {
					logger.error(ex.getMessage(), ex);
				}
			}
		}
		return prop;
	}

代码解读

  • propertyName指的是文件名,例如:system.properties
  • 实现文件读取的方式有至少5种 ,这里就不详细列举了。
  • https://www.cnblogs.com/hafiz/p/5876243.html

学习Java的同学注意了!!!
学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:543120397 我们一起学Java!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曾卫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值