`

Java Formatter

阅读更多
摘自:http://hi.baidu.com/banseon/blog/item/8a45c8ed09396b4e79f05579.html

System.out.printf


The first way you'll learn to work with the Formatter is not to interact with it directly, but instead to use the new PrintStream method printf. You're probably most familiar with PrintStream from doing System.out.println calls. Here is a simple example of the new printf method:

String initials = "jjl";
String comment   = "just because";
System.out.printf("reason: %s (noted by %s)", comment, initials);


The example prints the following output on the console:

reason: just because (noted by jjl)
In the example code, you are passing printf a format string that contains embedded format specifiers. You also pass it a list of arguments, each of which corresponds to a format specifier. There are two format specifiers in the example: %s and %s. The first corresponds to the comment argument, and the second corresponds to the initials argument.

A % in a format string designates the start of a format specifier. The end of a format specifier is triggered by one of many possible characters known as conversions. In the %s format specifier, the lowercase s indicates a String conversion. A string conversion means that Java calls toString on the corresponding argument, and substitutes the result for the format specifier.

String.format


If you only want to obtain a formatted string, but not print it, you can use the static method format on the String class. Here's an example that also demonstrates a few numeric conversions:

int a = 65;
String s = 
   String.format("char: %c integral: %d octal: %o hex: %x %n",
       a, a, a, a);


The %n at the end of the format string indicates a platform-specific line separator. When printed, the String s looks like this:

char: A integral: 65 octal: 101 hex: 41
Numeric conversions also support flags for padding, grouping, justification, and sign.

Note: There are four arguments, each using the reference a. Were you to supply only three, or were one of the arguments an invalid type (for example, a string), the format method would have generated one of a few runtime exceptions. This is unlike C, which ignores any formatting problems.

If you want to use the same argument more than once against a format string, you can use a shortcut:

String s = String.format("char: %c integral:%<d octal: %<o hex: %<x %n", a);


The a reference is specified only once. The less-than sign (<) in a format specifier means that it should use the same argument as the last format specifier.

Dates


Formatter provides an extensive number of date-related conversions. The following code:

String.format("%1$td %1$tb %1$ty", new Date())


produces a string with the value:

26 Feb 04
The 1$ in each format specifier is another way to designate which argument to use. In this example, all three format specifiers target the first argument.

The last part of each format specifier is how you do date conversions?using a two-character sequence starting with a t. The second character indicates the date part to convert. For example, td in the first specifier converts the day of the month.

java.util.Formatter



The Formatter class is at the core of the new formatting capability. It fully supports internationalization by letting you pass a Locale to the constructor; the other formatting methods (e.g. String.format) also allow this.

Formatter also lets you pass an Appendable object. This is a new interface that defines append methods so that the formatter can store its results in a text collector such as a stream object. Sun has modified all relevant Java classes, such as StringBuffer and PrintStream, to implement this interface. Here's how you might use it:

double avogadro      = 6.0e23;
StringBuffer buffer = new StringBuffer();
Formatter formatter = new Formatter(buffer, Locale.US);
formatter.format("avogadro's number: %e %n", avogadro);
formatter.format("base of the natural log: %e %n", Math.E);
System.out.println(buffer.toString());


The corresponding output:

[pre]avogadro's number:        6.000000e+23
base of the natural log: 2.718282e+00[/pre]
分享到:
评论

相关推荐

    eclipse java代码格式化 javaformatter20150123.zip

    标题中的“eclipse java代码格式化 javaformatter20150123.zip”指的是一个Eclipse插件,主要用于Java代码的格式化。这个插件的版本是20150123,可能包含了该时期最新的代码格式化规则和功能。在Eclipse IDE中,代码...

    eclispe中 java和js的Formatter

    在这里,你可以创建新的格式化配置,导入导出配置文件,比如我们压缩包中的`javaFormatter.xml`,这个文件包含了特定的格式化规则。 JavaScript Formatter则是针对JavaScript代码的类似工具,它确保了JS代码的格式...

    java-formatter:格式化Java代码的工具

    用法编译: mvn compile 跑步: mvn exec:java -Dexec.mainClass=me.tomassetti.javaformatter.JavaFormatter执照Apache许可2.0依存关系该工具基于 ,Maven会为您提供。 在我将它们发布到Maven Central之前,您需要...

    java-formatter.xml

    代码格式化配置文件

    java eclipse code formatter

    Java Eclipse Code Formatter是一款用于统一Java代码风格的强大工具,它整合在Eclipse集成开发环境中,能够帮助开发者按照预设的编码规范自动格式化代码,提升代码的可读性和团队协作效率。"Code Style"和"Code ...

    eclipse-formatter

    在Eclipse中,用户通常可以通过"Window" -&gt; "Preferences" -&gt; "Java" -&gt; "Code Style" -&gt; "Formatter"来配置和使用Formatter。在这里,你可以选择一个现有的代码格式配置,或者创建一个新的配置。配置项包括缩进方式...

    maven-java-formatter-plugin-0.3.jar

    maven-java-formatter-plugin-0.3.jar

    maven-java-formatter-plugin-0.4.jar

    maven-java-formatter-plugin-0.4.jar

    PrintStream,StringBuilder,Formatter

    在Java编程语言中,`PrintStream`, `StringBuilder` 和 `Formatter` 是三个非常重要的类,分别用于不同的输出处理。理解并熟练使用这三个类是提升Java编程能力的关键。 首先,我们来详细了解一下`PrintStream`。它...

    maven-java-formatter-plugin-0.4-sources.jar

    maven-java-formatter-plugin-0.4-sources.jar

    Java开发技术大全(500个源代码).

    HelloWorldApp.java 第一个用Java开发的应用程序。 firstApplet.java 第一个用Java开发的Applet小程序。 firstApplet.htm 用来装载Applet的网页文件 第2章 示例描述:本章介绍开发Java的基础语法知识。 ...

    eclipse-jee-photon-R-win32-x86_64.zip

    Java Formatter Debug JDT Developers PHP Development Tools General Fixed support for ASP tags Editor Formatter Debugger Platform Windows MacOS GTK3 Editors Dark Theme Other Open Source Projects Notices

    java格式化编码文件和使用说明

    导入方法通常是:在Eclipse的`Window`菜单中选择`Preferences`,然后找到`Java` -&gt; `Code Style` -&gt; `Formatter`,点击`Import`按钮导入`JavaFormatter.xml`。 `使用说明.txt`文件则详细介绍了如何使用这个自定义的...

    JAVA格式输出,JAVA格式输出

    在Java编程语言中,控制输出格式是通过使用`java.text`包中的`NumberFormat`类实现的。这个类提供了一组方法,使得我们能够格式化数字、货币值和百分比,以便它们按照特定的样式和区域设置显示。下面将详细讨论这些...

    java代码注释模板

    常见的Java代码格式化工具有Eclipse的`Source`菜单、IntelliJ IDEA的`Reformat Code`功能,以及命令行工具如Google的`Java Formatter`。 在实际开发中,良好的注释习惯和有效的格式化工具结合使用,不仅可以提升...

    java类Formatter解析.pdf

    Java类Formatter解析 Formatter是Java中的一个强大类库,它提供了强大且灵活的格式化功能,主要用于文本输出方面,例如数字、日期、金额等。以下是Formatter类的知识点总结: 1. Formatter类的功能:Formatter类...

    java代码格式化

    此外,有一些编辑器插件如VS Code的`Java Formatter`,Sublime Text的`Java Format`等,也能方便地进行代码格式化。 6. **代码重构**:格式化代码不仅是美化,也是重构的一部分。通过统一的格式,可以更容易地发现...

    Java自动生成实体类源代码

    - 使用代码格式化工具(如Google Java Formatter)保持代码风格一致。 通过自动生成实体类,开发者可以专注于业务逻辑,而不是重复的代码编写,从而提高生产力,降低错误率。对于大型项目,这种自动化实践更是不可...

    eclipse java code Format模版

    Eclipse社区还开发了许多第三方代码格式化插件,如"Google Java Formatter",提供了更严格的代码风格,可以替代Eclipse默认的代码格式化工具。安装插件后,可以根据插件提供的设置进行配置。 总之,Eclipse Java ...

Global site tag (gtag.js) - Google Analytics