Jennifer
Jennifer

Reputation: 23

Java : Getting a value from a properties file

I'm using the <liferay-ui:message key="username" /> to get some data from my property file in my portlet.

Is there a Java code equivalent for this tag ?

Thank you.

Upvotes: 2

Views: 10350

Answers (3)

Jonny
Jonny

Reputation: 2683

Yes, it's PortletProps.get(String key).

Hope this helps!

~~ EDIT ~~

The above as Sandeep has pointed out isn't the equivalent of what liferay-ui:message does, but it is the method to retrieve values from a portlet.properties file.

As Sandeep has said you should use LanguageUtil to replicate the functionality in Java code.

Upvotes: 1

Sandeep Nair
Sandeep Nair

Reputation: 3660

Actually the question title does not go with question content. To read from portlet.properties you have to do as what Jonny said. But on seeing the content of the question, I assume that what you want is the java code equivalent of the tag output that you have mentioned.

liferay-ui:message DOES NOT read the value from portlet.properties file so PortletProps will not work if that is what you are expecting as it is meant to read value only from portlet.properties and not Language.properties.

You should use methods of LanguageUtil class to get the value.

Upvotes: 2

korifey
korifey

Reputation: 3519

If you need merely read property from property file you can:

Properties p = new Properties();
p.load(new FileInputStream("file_with.properties"));
String message = p.getProperty("username");

Upvotes: -1

Related Questions