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();
		}
	}
}


猜你喜欢

转载自blog.csdn.net/u013332981/article/details/80016125