您的位置:首页 > 运维架构

获得Properties文件的内容

2015-06-16 09:58 316 查看
package com.meyacom.emailUtil;

import java.io.IOException;

import java.io.InputStream;

import j
a98e
ava.util.Properties;

public class SendoutEmail{

 private static String toemailname;

 private static String fromemailname;

 private static String fromemailpass;

 

 public  SendoutEmail() {

  String resources = "email.properties";

  // 将配置文件加载单独写成一个函数,将异常处理进行封装,使代码整洁

  Properties properties = loadProperties(resources);

  // 直接用getProperty获取属性值

  this.toemailname = properties.getProperty("toemailname");

  this.fromemailname = properties.getProperty("fromemailname");

  this.fromemailpass = properties.getProperty("fromemailpass");

 }

 private Properties loadProperties(String resources) {

    // 使用InputStream得到一个资源文件

    //InputStream inputstream = this.getClass().getResourceAsStream(resources);

    InputStream inputstream = this.getClass().getClassLoader().getResourceAsStream(resources);

    // new 一个Properties

    Properties properties = new Properties();

    try {

    // 加载配置文件

       properties.load(inputstream);

       return properties;

    } catch (IOException e) {

     e.printStackTrace();

    } finally {

       try {

          inputstream.close();

       } catch (IOException e) {

          throw new RuntimeException(e);

       }

    }

    return properties;

 }

 public static String getToemailname() {

  return toemailname;

 }

 public static void setToemailname(String toemailname) {

  SendoutEmail.toemailname = toemailname;

 }

 public static String getFromemailname() {

  return fromemailname;

 }

 public static void setFromemailname(String fromemailname) {

  SendoutEmail.fromemailname = fromemailname;

 }

 public static String getFromemailpass() {

  return fromemailpass;

 }

 public static void setFromemailpass(String fromemailpass) {

  SendoutEmail.fromemailpass = fromemailpass;

 }

 

 

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: