vinayaka
vinayaka

Reputation: 39

Properties file in java

I am trying to run the below sample code in java.

import java.util.Locale;  
import java.util.ResourceBundle;

public class InternationalizationDemo {  

  public static void main(String[] args) {        
    ResourceBundle bundle = ResourceBundle.getBundle("MessageBundle", Locale.CANADA_FRENCH);  
    System.out.println("Message in "+Locale.CANADA_FRENCH +":"+bundle.getString("greeting"));
  }
}

1.The above code executes properly when MessageBundle.properties is placed in the class path. But I want to execute the above code successfully by removing the MessageBundle.properties from the classpath and placing it in some other location. How can I do this? Thanks in Advance.

Upvotes: 1

Views: 1035

Answers (2)

Hrabosch
Hrabosch

Reputation: 1583

You can load properties file externally by this:

// Path to your file, if you have it on local, use something like C:\\MyFolder or \home\usr\etc
File file = new File("YOUR_PATH");
URL[] url = {file.toURI().toURL()};
ResourceBundle rb = ResourceBundle.getBundle("MessageBundle", Locale.CANADA_FRENCH, new URLClassLoader(url));

Example is from here: https://coderanch.com/t/432762/java/java/absolute-path-bundle-file

You are able to use remote Properties file or file which is saved on local.

Upvotes: 1

Nicolas Filotto
Nicolas Filotto

Reputation: 45005

You could use PropertyResourceBundle and get the path of your properties file from a System property for example something like that:

String configPath = System.getProperty("config.path");
ResourceBundle bundle = new PropertyResourceBundle(new FileReader(configPath));

Then in your launch command you will need to add -Dconfig.path=/path/to/my/config.properties

Upvotes: 2

Related Questions