简体   繁体   English

从属性文件读取内容

[英]Reading contents from property file

I'm having issues in reading values from property file. 我在从属性文件读取值时遇到问题。 In my project, there is a folder called "env". 在我的项目中,有一个名为“ env”的文件夹。 Inside that there are three folders named "default", "dev" and "staging". 里面有三个文件夹,分别是“默认”,“开发”和“登台”。 Those three folders contain three property files named "default.properties", "dev.properties" and "staging.properties". 这三个文件夹包含三个名为“ default.properties”,“ dev.properties”和“ staging.properties”的属性文件。 Inside all of those three property files, I have the below content: 在所有这三个属性文件中,我具有以下内容:

# 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

在此处输入图片说明

Now what I am doing is reading them using java. 现在,我正在使用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");
    }
}

In the console, I am getting the output as null 在控制台中,我得到的输出为null

Seems like you are couple of steps behind to achieve same. 似乎您在实现相同目标方面落后了几步。

  1. you have 3 different properties files as per env. 每个环境都有3个不同的属性文件。 so you need one mapper to map same. 因此,您需要一个映射器来映射相同的映射器。
  2. then load property file something like: 然后加载属性文件,例如:

     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 } 

It should work. 它应该工作。 (I did not test but I don't see any reason for failure). (我没有进行测试,但看不到任何失败的原因)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM