【问题标题】:Reading contents from property file从属性文件中读取内容
【发布时间】:2018-03-31 07:50:34
【问题描述】:

我在从属性文件中读取值时遇到问题。在我的项目中,有一个名为“env”的文件夹。里面有三个文件夹,分别命名为“default”、“dev”和“staging”。这三个文件夹包含三个名为“default.properties”、“dev.properties”和“staging.properties”的属性文件。在所有这三个属性文件中,我有以下内容:

# Emailing configurations
sender_email_address = osanda.nimalarathna@maxsoft.com
sender_email_password = 1qaz2wsx@
recipients_email_addresses = eranga.heshan@maxsoft.com
email_subject = MaxSoft IntelliAPI Email Test

现在我正在做的是使用 java 阅读它们。

package com.maxsoft.ata.util;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Email {

    private static final String SENDER_EMAIL_ADDRESS = System.getenv("sender_email_address");
    private static final String SENDER_EMAIL_PASSWORD = System.getenv("sender_email_password");
    private static final String RECIPIENTS_EMAIL_ADDRESSES = System.getenv("recipients_email_addresses");
    private static final String EMAIL_SUBJECT = System.getenv("email_subject");

    public static void send(String messageBody) {

        System.out.println(SENDER_EMAIL_ADDRESS);

        /**
         * Email sending codes
         */

        }

    public static void main(String[] args) {
        send("test message");
    }
}

在控制台中,我得到的输出为null

【问题讨论】:

标签: java oop embedded-resource properties-file


【解决方案1】:

似乎你落后了几个步骤才能实现同样的目标。

  1. 根据 env,您有 3 个不同的属性文件。所以你需要一个映射器来映射相同的。
  2. 然后加载类似的属性文件:

    InputStream is = null;
    try {
        this.prop = new Properties();
        is = this.getClass().getResourceAsStream("your env specific property file");
        prop.load(is);
    } catch (FileNotFoundException e) {
       //handle exception
    } catch (IOException e) {
         //handle exception
    }
    

它应该工作。 (我没有测试,但我没有看到任何失败的原因)。

【讨论】:

  • 帮助close()InputStream。
猜你喜欢
  • 2023-02-03
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
  • 1970-01-01
  • 2020-11-11
相关资源
最近更新 更多