Replace a properties file placeholder

Source: Internet
Author: User

Read the properties file first, and then use Messageformat to format the string.

The Messageformat-java.text.messageformat class allows you to replace a portion of a message string (in this case, a message from a resource bundle) with the parameters specified by the runtime. This is useful in situations where you create a sentence, but the words appear in different order in different languages. The placeholder string {0} in the message is replaced with the first run-time parameter, {1} is replaced with the second run-time parameter, and so on.

To do this, you must first create two resource bundles
The format of this particular message is defined as "{0} while loading: {1}" and the second bundle is in the format "{1} loaded unsuccessfully: {0}". {0} represents a placeholder for the argument to be substituted for the message. Placeholders start at 0 and increase.

Use the Messageformat.format () method when you replace the argument and actually create the message to display. This takes two arguments, the first is the message to be formatted, and the second is the argument Object []. The following shows all the code:

Import java.text.*;
public class Format-it {
public static void Main (String args[]) {
String format1 = "{0} while loading: {1}";
String Format2 = "{1} loaded unsuccessfully: {0}";
String exceptionname = "I/o Exception";
String filename = "Foobar.java";
Object [] Fmtargs = {exceptionname, filename};
System.out.println (
Messageformat.format (FORMAT1, Fmtargs));
System.out.println (
Messageformat.format (FORMAT2, Fmtargs));
}
}

The output that runs is as follows:

Foobar.java while LOADING:I/O Exception
I/O Exception loaded Unsuccessfully:Foobar.java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.