java.util.Formatter.<init>()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.6k)|赞(0)|评价(0)|浏览(189)

本文整理了Java中java.util.Formatter.<init>()方法的一些代码示例,展示了Formatter.<init>()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Formatter.<init>()方法的具体详情如下:
包路径:java.util.Formatter
类名称:Formatter
方法名:<init>

Formatter.<init>介绍

[英]Constructs a Formatter.

The output is written to a StringBuilder which can be acquired by invoking #out() and whose content can be obtained by calling toString.

The Locale used is the user's default locale. See "Be wary of the default locale".
[中]构造一个格式化程序。
输出被写入StringBuilder,该StringBuilder可以通过调用#out()获得,其内容可以通过调用toString获得。
使用的区域设置是用户的默认区域设置。请参阅“{$0$}”。

代码示例

代码示例来源:origin: stackoverflow.com

public String toDecimalString() {
  Formatter f = new Formatter();
  for(int digit : digits) {
    f.format("%09d", digit);
  }
  return f.toString();
}

代码示例来源:origin: knowm/XChange

private static String byteToHex(final byte[] hash) {
  Formatter formatter = new Formatter();
  for (byte b : hash) {
   formatter.format("%02x", b);
  }
  String result = formatter.toString();
  formatter.close();
  return result;
 }
}

代码示例来源:origin: org.assertj/assertj-core

private static String escapeUnicode(String input) {
  StringBuilder b = new StringBuilder(input.length());
  Formatter formatter = new Formatter(b);
  for (char c : input.toCharArray()) {
   if (c < 128) {
    b.append(c);
   } else {
    formatter.format("\\u%04x", (int) c);
   }
  }
  formatter.close();
  return b.toString();
 }
}

代码示例来源:origin: wildfly/wildfly

public JBossLogPrintWriter format(Locale l, String format, Object ... args) {
  synchronized (lock) {
    if ((formatter == null) || (formatter.locale() != l))
      formatter = new Formatter(this, l);
    formatter.format(l, format, args);
  }
  return this;
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testDefaultAppend() {
  assertEquals("foo", FormattableUtils.append("foo", new Formatter(), 0, -1, -1).toString());
  assertEquals("fo", FormattableUtils.append("foo", new Formatter(), 0, -1, 2).toString());
  assertEquals(" foo", FormattableUtils.append("foo", new Formatter(), 0, 4, -1).toString());
  assertEquals("   foo", FormattableUtils.append("foo", new Formatter(), 0, 6, -1).toString());
  assertEquals(" fo", FormattableUtils.append("foo", new Formatter(), 0, 3, 2).toString());
  assertEquals("   fo", FormattableUtils.append("foo", new Formatter(), 0, 5, 2).toString());
  assertEquals("foo ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1).toString());
  assertEquals("foo   ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1).toString());
  assertEquals("fo ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2).toString());
  assertEquals("fo   ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2).toString());
}

代码示例来源:origin: apache/hive

Formatter fmt = new Formatter(sb);
if (doCSV) {
 result.displayCSV(fmt, csvSeparator);
 Formatter f = new Formatter(s);
 result.display(f);
 System.out.print(s);
 f.close();
fmt.close();

代码示例来源:origin: stackoverflow.com

public String toDecimalString() {
  Formatter f = new Formatter();
  f.format("%d", digits[0]);
  for(int i = 1 ; i < digits.length; i++) {
    f.format("%09d", digits[i]);
  }
  return f.toString();
}

代码示例来源:origin: stackoverflow.com

Formatter formatter = new Formatter();
for (byte b : hash)
  formatter.format("%02x", b);
String result = formatter.toString();
formatter.close();
return result;

代码示例来源:origin: joel-costigliola/assertj-core

private static String escapeUnicode(String input) {
  StringBuilder b = new StringBuilder(input.length());
  Formatter formatter = new Formatter(b);
  for (char c : input.toCharArray()) {
   if (c < 128) {
    b.append(c);
   } else {
    formatter.format("\\u%04x", (int) c);
   }
  }
  formatter.close();
  return b.toString();
 }
}

代码示例来源:origin: wildfly/wildfly

public JBossLogPrintWriter format(String format, Object ... args) {
  synchronized (lock) {
    if ((formatter == null)
      || (formatter.locale() != Locale.getDefault()))
      formatter = new Formatter(this);
    formatter.format(Locale.getDefault(), format, args);
  }
  return this;
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testAlternatePadCharacter() {
  final char pad='_';
  assertEquals("foo", FormattableUtils.append("foo", new Formatter(), 0, -1, -1, pad).toString());
  assertEquals("fo", FormattableUtils.append("foo", new Formatter(), 0, -1, 2, pad).toString());
  assertEquals("_foo", FormattableUtils.append("foo", new Formatter(), 0, 4, -1, pad).toString());
  assertEquals("___foo", FormattableUtils.append("foo", new Formatter(), 0, 6, -1, pad).toString());
  assertEquals("_fo", FormattableUtils.append("foo", new Formatter(), 0, 3, 2, pad).toString());
  assertEquals("___fo", FormattableUtils.append("foo", new Formatter(), 0, 5, 2, pad).toString());
  assertEquals("foo_", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1, pad).toString());
  assertEquals("foo___", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1, pad).toString());
  assertEquals("fo_", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2, pad).toString());
  assertEquals("fo___", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2, pad).toString());
}

代码示例来源:origin: jersey/jersey

@Override
  public String toString() {
    return (new Formatter()).format(
        "{\"flightId\":\"%s\",\"company\":\"%s\",\"number\":%d,\"aircraft\":\"%s\"}",
        this.flightId, this.company, this.number, this.aircraft).toString();
  }
}

代码示例来源:origin: opentripplanner/OpenTripPlanner

private List<URL> getDownloadURLsCached() {
  Envelope extent = graph.getExtent();
  Formatter formatter = new Formatter();
  String filename = formatter.format("%f,%f-%f,%f.urls", extent.getMinX(), extent.getMinY(),
      extent.getMaxX(), extent.getMaxY()).toString();
  formatter.close();
  try {
    File file = new File(cacheDirectory, filename);

代码示例来源:origin: org.apache.poi/poi

private void writeSingleInteger(String fmt, int num, StringBuffer output, List<Special> numSpecials, Set<CellNumberStringMod> mods) {
  StringBuffer sb = new StringBuffer();
  Formatter formatter = new Formatter(sb, locale);
  try {
    formatter.format(locale, fmt, num);
  } finally {
    formatter.close();
  }
  writeInteger(sb, output, numSpecials, mods, false);
}

代码示例来源:origin: patric-r/jvmtop

/**
  * Formats number of milliseconds to a HH:MM representation
  *
  * TODO: implement automatic scale (e.g. 1d 7h instead of 31:13m)
  * @param millis
  * @return
  */
 public String toHHMM(long millis)
 {
  StringBuilder sb = new StringBuilder();
  Formatter formatter = new Formatter(sb);
  formatter
.format("%2d:%2dm", millis / 1000 / 3600,
    (millis / 1000 / 60) % 60);
  return sb.toString();
 }

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testEllipsis() {
  assertEquals("foo", FormattableUtils.append("foo", new Formatter(), 0, -1, -1, "*").toString());
  assertEquals("f*", FormattableUtils.append("foo", new Formatter(), 0, -1, 2, "*").toString());
  assertEquals(" foo", FormattableUtils.append("foo", new Formatter(), 0, 4, -1, "*").toString());
  assertEquals("   foo", FormattableUtils.append("foo", new Formatter(), 0, 6, -1, "*").toString());
  assertEquals(" f*", FormattableUtils.append("foo", new Formatter(), 0, 3, 2, "*").toString());
  assertEquals("   f*", FormattableUtils.append("foo", new Formatter(), 0, 5, 2, "*").toString());
  assertEquals("foo ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1, "*").toString());
  assertEquals("foo   ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1, "*").toString());
  assertEquals("f* ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2, "*").toString());
  assertEquals("f*   ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2, "*").toString());
  assertEquals("foo", FormattableUtils.append("foo", new Formatter(), 0, -1, -1, "+*").toString());
  assertEquals("+*", FormattableUtils.append("foo", new Formatter(), 0, -1, 2, "+*").toString());
  assertEquals(" foo", FormattableUtils.append("foo", new Formatter(), 0, 4, -1, "+*").toString());
  assertEquals("   foo", FormattableUtils.append("foo", new Formatter(), 0, 6, -1, "+*").toString());
  assertEquals(" +*", FormattableUtils.append("foo", new Formatter(), 0, 3, 2, "+*").toString());
  assertEquals("   +*", FormattableUtils.append("foo", new Formatter(), 0, 5, 2, "+*").toString());
  assertEquals("foo ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1, "+*").toString());
  assertEquals("foo   ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1, "+*").toString());
  assertEquals("+* ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2, "+*").toString());
  assertEquals("+*   ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2, "+*").toString());
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Log a printf-style formatted message to the channels specified in this RedwoodChannels object.
 * @param format The format string for the printf function
 * @param args The arguments to the printf function
 */
public void logf(String format, Object... args) {
 log((Supplier<String>) () -> new Formatter().format(format, args).toString());
}

代码示例来源:origin: tabulapdf/tabula-java

@Override
public String toString() {
  StringBuilder sb = new StringBuilder();
  Formatter formatter = new Formatter(sb);
  String rv = formatter.format("%s[x1=%f y1=%f x2=%f y2=%f]", this.getClass().toString(), this.x1, this.y1, this.x2, this.y2).toString();
  formatter.close();
  return rv;
}

代码示例来源:origin: org.apache.poi/poi

/** {@inheritDoc} */
public void formatValue(StringBuffer toAppendTo, Object value) {
  double elapsed = ((Number) value).doubleValue();
  if (elapsed < 0) {
    toAppendTo.append('-');
    elapsed = -elapsed;
  }
  Object[] parts = new Long[specs.size()];
  for (int i = 0; i < specs.size(); i++) {
    parts[i] = specs.get(i).valueFor(elapsed);
  }
  Formatter formatter = new Formatter(toAppendTo, Locale.ROOT);
  try {
    formatter.format(printfFmt, parts);
  } finally {
    formatter.close();
  }
}

代码示例来源:origin: neo4j/neo4j

private static String determineMacAddress()
{
  String formattedMac = "0";
  try
  {
    InetAddress address = InetAddress.getLocalHost();
    NetworkInterface ni = NetworkInterface.getByInetAddress( address );
    if ( ni != null )
    {
      byte[] mac = ni.getHardwareAddress();
      if ( mac != null )
      {
        StringBuilder sb = new StringBuilder( mac.length * 2 );
        Formatter formatter = new Formatter( sb );
        for ( byte b : mac )
        {
          formatter.format( "%02x", b );
        }
        formattedMac = sb.toString();
      }
    }
  }
  catch ( Throwable t )
  {
    //
  }
  return formattedMac;
}

相关文章