`

java MessageFormat

    博客分类:
  • java
阅读更多

 

一。

MessageFormat 提供了以与语言无关方式生成连接消息的方式。使用此方法构造向终端用户显示的消息。

MessageFormat 获取一组对象,格式化这些对象,然后将格式化后的字符串插入到模式中的适当位置。

注: MessageFormat 不同于其他 Format 类,因为 MessageFormat 对象是用其构造方法之一创建的(而不是使用 getInstance MessageFormat 本身不实现特定于语言环境的行为。特定于语言环境的行为是由所提供的模式和用于已插入参数的子格式来定义的。

 

单引号的时候 如 String a = "abc'{0}'abc"; 必须改成 String a = "abc''{0}''abc";

 

 

MessageFormat运行开发者输出文本中的变量的格式。它是一个强大的类,就像下面的例子展示的那样:

String message =
  "Once upon a time ({1,date}, around about {1,time,short}), there " +
  "was a humble developer named Geppetto who slaved for " +
  "{0,number,integer} days with {2,number,percent} complete user " +
  "requirements. ";
  Object[ ] variables = new Object[ ]
  { new Integer(4), new Date( ), new Double(0.21) }
  String output = MessageFormat.format( message, variables );
  System.out.println(output); 
 

  隐藏在信息中的是描述输出的格式的一种短小的代码,范例的输出如下:
  
  Once upon a time (Nov 3, 2002, around about 1:35 AM), there was a humble developer
  named Geppetto who slaved for 4 days with 21% complete user requirements.
  假如相同的信息需要被重复输出但是变量的值不同,那么创建一个MessageFormat对象并给出信息。下面是上面的例子的修正版:
  

//String output = MessageFormat.format(message, variables );
  //变为:
  MessageFormat formatter = new MessageFormat(message);
  String output = formatter.format(variables); 
 

  
  除了可以处理日期、时间、数字和百分数外,MessageFormat也可以处理货币,运行更多的数字格式的控制并且答应指定ChoiceFormat。
  
   MessageFormat是一个极好的类,它应该经常被使用但是现在还没有。它的最大的缺点是数据是被作为变量传递而不是一个Properties对 象。一个简单的解决办法是写一个封装类,它会预解析字符串为格式化的结果,将Properties的key转换为一个数组索引,顺序是 Properties.keys( )返回的顺序。

 

二。例子

<!-- Generated by javadoc (build 1.6.0-beta2) on Fri Mar 09 12:51:16 CST 2007 -->

第一个例子 使用静态的方法 MessageFormat.format ,它在内部创建一个只使用一次的 MessageFormat

int planet = 7;
 String event = "a disturbance in the Force";

 String result = MessageFormat.format(
     "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
     planet, new Date(), event);
 

输出为:

 At 12:30 PM on Jul 3, 2053, there was a disturbance in the Force on planet 7.
 

下面的例子创建了一个可以重复使用的 MessageFormat 实例:

int fileCount = 1273;
 String diskName = "MyDisk";
 Object[] testArgs = {new Long(fileCount), diskName};

 MessageFormat form = new MessageFormat(
     "The disk \"{1}\" contains {0} file(s).");

 System.out.println(form.format(testArgs));
 

不同 fileCount 值的输出:

 The disk "MyDisk" contains 0 file(s).
 The disk "MyDisk" contains 1 file(s).
 The disk "MyDisk" contains 1,273 file(s).
 

对于更复杂的模式,可以使用 ChoiceFormat 来生成正确的单数和复数形式:

 MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
 double[] filelimits = {0,1,2};
 String[] filepart = {"no files","one file","{0,number} files"};
 ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
 form.setFormatByArgumentIndex(0, fileform);

 int fileCount = 1273;
 String diskName = "MyDisk";
 Object[] testArgs = {new Long(fileCount), diskName};

 System.out.println(form.format(testArgs));

 

不同的 fileCount 值的输出:

 The disk "MyDisk" contains no files.
 The disk "MyDisk" contains one file.
 The disk "MyDisk" contains 1,273 files.
 

如上例所示,可以以编程方式来创建 ChoiceFormat ,或使用模式创建。有关更多信息,请参阅 ChoiceFormat

 form.applyPattern(
    "There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.");
 

注: 从上面的例子可以看到,由 MessageFormat 中的 ChoiceFormat 所生成的字符串要进行特殊处理;'{' 的出现用来指示子格式,并导致递归。如果 MessageFormatChoiceFormat 都是以编程方式创建的(而不是使用字符串模式),那么要注意不要生成对其自身进行递归的格式,这将导致无限循环。

当一个参数在字符串中被多次解析时,最后的匹配将是解析的最终结果。例如,

 MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}");
 Object[] objs = {new Double(3.1415)};
 String result = mf.format( objs );
 // result now equals "3.14, 3.1"
 objs = null;
 objs = mf.parse(result, new ParsePosition(0));
 // objs now equals {new Double(3.1)}
 

同样,使用包含同一参数多个匹配项的模式对 MessageFormat 对象进行解析时将返回最后的匹配。例如,

MessageFormat mf = new MessageFormat("{0}, {0}, {0}");
 String forParsing = "x, y, z";
 Object[] objs = mf.parse(forParsing, new ParsePosition(0));
 // result now equals {new String("z")}

 

 

同步

消息格式不是同步的。建议为每个线程创建独立的格式实例。如果多个线程同时访问一个格式,则它必须是外部同步的。

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics