简体   繁体   中英

Reading contents from property file

I'm having issues in reading values from property file. In my project, there is a folder called "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". 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.

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

Seems like you are couple of steps behind to achieve same.

  1. you have 3 different properties files as per env. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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