Dynamically read / add value to the conf parameter of a file using properties

I have a message as shown below in my conf file.

text.message = Richardmust go Schoolto 01/06/2012/ 1days.

All highlighted field will be variable.

I want to read this line text.meand insert a value from my java using "Properties".

I know how to read the entire string using Prop, but I don’t know how to read the above String, which will look like.

text.message = #name# should go to # place # at # date # / # days #.

  • how can I read the above line from conf using properties and insert data dynamically?

  • It can be a date or days in a row. How can I turn these options on and off?

Thank.

+5
source share
3 answers

API MessageFormat.

Kickoff:

text.message = {0} has to go to {1} in {2,date,dd/MM/yyyy} / {3}

String message = properties.getProperty("text.message");
String formattedMessage = MessageFormat.format(message, "Richard", "School", new Date(), "1days");
System.out.println(formattedMessage); // Richard has to go to School in 31/05/2012 / 1days
+15

MessageFormat, .

, ...

String pattern = "{0} has to go to {1} in {2,date} / {3,number,integer} days.";
String result = MessageFormat.format(pattern, "Richard", "school", new Date(), 5);
System.out.println(result);

... :

Richard has to go to school in 31-May-2012 / 5 days.

Properties, MessageFormat.

+4

You can try this code to get help on the properties file.

App.java

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

public class App {
public static void main(String[] args) {

Properties prop = new Properties();
OutputStream output = null;

try {

    output = new FileOutputStream("config.properties");

    // set the properties value
    prop.setProperty("database", "localhost");
    prop.setProperty("dbuser", "ayushman");
    prop.setProperty("dbpassword", "password");

    // save properties to project root folder
    prop.store(output, null);

} catch (IOException io) {
    io.printStackTrace();
} finally {
    if (output != null) {
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
 }
}

Output

config.properties

#Fri Jan 17 22:37:45 MYT 2014
dbpassword=password
database=localhost
dbuser=ayushman
-1
source

All Articles