java如何读取配置文件

最近做了一个审批过程中邮件提醒审批人的功能,遇到这样一个情况,在这个邮箱工具类中,我用的自己的163邮箱做的测试。


测试没问题之后,说明功能已经实现了。但是我总不能让别人使用这个功能的时候用我的邮箱去发送邮件,所以这里面涉及到邮箱的配置统统需要可变。而且可能不是163邮件,所以,这里介绍以下,把这些需要改变的数据放在配置文件properties中。

在src下新建一文件,如email.properties:


这里我是根据自己的需要配置了一些(下面的配置是我针对不同的情况可以配置打开或者关闭邮件功能)。

那么java代码要怎么去读取这个文件呢。代码如下:

import java.util.ResourceBundle;

public class MailManagerUtil {
	public static String hostName;
	public static Integer smtpPort;
	public static String authenticaticatorAcc;
	public static String authenticaticatorpwd;
	public static String from;
	public static String news_switch;
	public static String form4_switch;
	public static String work_switch;
	public static String work_feedback_switch;
	public static String work_confirm_switch;
	public static String xf_suggestion_switch;
	public static String xf_suggestion_report_switch;
	public static String xf_care_educatio_switch;
	public static String xf_care_educatio_report_switch;
	
	
	static {
		try{
			ResourceBundle res = ResourceBundle.getBundle("email");
			hostName = res.getString("hostName");
			smtpPort = Integer.parseInt(res.getString("smtpPort"));
			authenticaticatorAcc = res.getString("authenticaticatorAcc");
			authenticaticatorpwd = res.getString("authenticaticatorpwd");
			from = res.getString("from");
			news_switch = res.getString("news_switch");
			form4_switch = res.getString("form4_switch");
			work_switch = res.getString("work_switch");
			work_feedback_switch = res.getString("work_feedback_switch");
			work_confirm_switch = res.getString("work_confirm_switch");
			xf_suggestion_switch = res.getString("xf_suggestion_switch");
			xf_suggestion_report_switch = res.getString("xf_suggestion_report_switch");
			xf_care_educatio_switch = res.getString("xf_care_educatio_switch");
			xf_care_educatio_report_switch = res.getString("xf_care_educatio_report_switch");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hidetou

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

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

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

打赏作者

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

抵扣说明:

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

余额充值