下面列出了java.util.Formatter#out ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Can be used to append a String to a formatter.
*
* @param formatter
* The {@link java.util.Formatter Formatter}
* @param width
* Minimum width to meet, filled with space if needed
* @param precision
* Maximum amount of characters to append
* @param leftJustified
* Whether or not to left-justify the value
* @param out
* The String to append
*/
public static void appendTo(Formatter formatter, int width, int precision, boolean leftJustified, String out)
{
try
{
Appendable appendable = formatter.out();
if (precision > -1 && out.length() > precision)
{
appendable.append(Helpers.truncate(out, precision));
return;
}
if (leftJustified)
appendable.append(Helpers.rightPad(out, width));
else
appendable.append(Helpers.leftPad(out, width));
}
catch (IOException e)
{
throw new UncheckedIOException(e);
}
}
protected void appendFormat(Formatter formatter, int width, int precision, boolean leftJustified, String out)
{
try
{
Appendable appendable = formatter.out();
if (precision > -1 && out.length() > precision)
{
appendable.append(Helpers.truncate(out, precision - 3)).append("...");
return;
}
if (leftJustified)
appendable.append(Helpers.rightPad(out, width));
else
appendable.append(Helpers.leftPad(out, width));
}
catch (IOException e)
{
throw new UncheckedIOException(e);
}
}
/**
* java.util.Formatter#Formatter(Appendable)
*/
public void test_ConstructorLjava_lang_Appendable() {
MockAppendable ma = new MockAppendable();
Formatter f1 = new Formatter(ma);
assertEquals(ma, f1.out());
assertEquals(f1.locale(), Locale.getDefault());
assertNotNull(f1.toString());
Formatter f2 = new Formatter((Appendable) null);
/*
* If a(the input param) is null then a StringBuilder will be created
* and the output can be attained by invoking the out() method. But RI
* raises an error of FormatterClosedException when invoking out() or
* toString().
*/
Appendable sb = f2.out();
assertTrue(sb instanceof StringBuilder);
assertNotNull(f2.toString());
}