- 浏览: 1065598 次
- 性别:
- 来自: 南昌
文章分类
- 全部博客 (276)
- 生活 (1)
- 代码之美 (22)
- Media (7)
- Android Widget (3)
- Android Intent (1)
- Android Activity (4)
- UI event handle--UI事件处理机制 (2)
- Java基础知识 (12)
- android Databases (5)
- Android 系统知识 (70)
- 平常遇到的问题与解决方法 (38)
- Android TextView/EditView (2)
- Thinking Java (1)
- android webkit (6)
- JSON (1)
- XML (4)
- HTTP (1)
- Google Weather API (1)
- android 2.3 NFC (10)
- android app (20)
- android framework (7)
- C++ (2)
- android System (5)
- Pthread (1)
- Wifi (8)
- Unix/Linux C (8)
- Android 4.0 (1)
- Mail (1)
- Smack 源码学习 (4)
- iOS (4)
- Android (1)
- git (1)
- Gallery3d (2)
- React-Natice (1)
最新评论
-
dd18349182956:
你是用的smack哪个版本?我用的smack4.1.3和sma ...
关于socket长连接的心跳包 -
xukaiyin:
全英文
getApplicationContext()与this,getBaseContext() -
裂风矢:
...
<category android:name="android.intent.category.DEFAULT" /> 惹的祸 -
xanthodont:
mark一下
XMPP——Smack -
Evilover3:
mark一下,学习了
XMPP——Smack
摘自: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]
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]
发表评论
-
ContentProvider --Call方法
2018-04-03 16:43 3768很早之前接触ContentProvider知道它提供的方法就是 ... -
android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a read
2015-11-27 14:50 2954android.database.sqlite.SQLiteR ... -
形成一个Resource Uri
2011-02-28 09:51 2968形成一个Resource Uri: public Uri ... -
getStringArray
2011-02-26 11:25 4222将String集放在String的resource里面:(Qu ... -
如何让你的list不被修改
2011-02-25 16:52 1486今天在看QuickSearchBox的源码:发现了可以对lis ... -
按Back key把Activity关掉即使有键盘的时候
2011-02-21 10:38 3088一般情况下,当键盘显示的时候,你按下back只会将键盘关掉,但 ... -
R.bool.use_32bit
2011-02-09 16:49 1964很久没有写新的文章,自己懒了吧,没了动力。今天还是耐住了惰性, ... -
让class只有一个实例的例子
2010-10-25 16:49 1809最近在学习android 的webkit,看到一些自己认为是好 ... -
滚动 TextView
2010-04-16 11:39 1297http://bbs.lupa.gov.cn/home/spa ... -
查询Internal 或External的容量
2010-04-14 21:04 1245我们可通过StatF来查询Internal或External的 ... -
关闭android程序
2010-03-17 17:17 1659转自:http://mingkg21.iteye.com/bl ... -
自定义android Intent Action与继承TextView形成一个动态改变text的View
2010-03-12 17:35 4712//Activity package com.android. ... -
获得手机系统设置的区域
2010-03-09 19:45 2020通过Locate这个类,我们可以获得手机系统设置的区域: ja ... -
关于android获得图片的总结
2010-03-08 14:50 63721,已将图片保存到drawable目录下 [color=cya ... -
android 视频的缩图制作
2010-03-05 15:46 2818在Gallery 这支ap里我们可 ... -
RandomAccessFile
2010-03-05 15:00 4050随机访问类(RandomAccessFile) - [] 输 ... -
Java iterator
2010-02-27 11:29 1489摘自[url]http://hi.baidu.com/heyi ... -
检查盘volumn的容量
2010-02-26 10:57 1413通过StatFs可以获得Filesystem 的状态 Str ... -
检查盘volumn是否可以写入
2010-02-26 10:44 980通过创建一个临时文件来确定是否盘可写。注意不要把文件放在根目录 ... -
Thread
2010-02-26 10:08 1901android 对UI的操作只能在UI线程(一般是在Activ ...
相关推荐
标题中的“eclipse java代码格式化 javaformatter20150123.zip”指的是一个Eclipse插件,主要用于Java代码的格式化。这个插件的版本是20150123,可能包含了该时期最新的代码格式化规则和功能。在Eclipse IDE中,代码...
在这里,你可以创建新的格式化配置,导入导出配置文件,比如我们压缩包中的`javaFormatter.xml`,这个文件包含了特定的格式化规则。 JavaScript Formatter则是针对JavaScript代码的类似工具,它确保了JS代码的格式...
用法编译: mvn compile 跑步: mvn exec:java -Dexec.mainClass=me.tomassetti.javaformatter.JavaFormatter执照Apache许可2.0依存关系该工具基于 ,Maven会为您提供。 在我将它们发布到Maven Central之前,您需要...
代码格式化配置文件
Java Eclipse Code Formatter是一款用于统一Java代码风格的强大工具,它整合在Eclipse集成开发环境中,能够帮助开发者按照预设的编码规范自动格式化代码,提升代码的可读性和团队协作效率。"Code Style"和"Code ...
在Eclipse中,用户通常可以通过"Window" -> "Preferences" -> "Java" -> "Code Style" -> "Formatter"来配置和使用Formatter。在这里,你可以选择一个现有的代码格式配置,或者创建一个新的配置。配置项包括缩进方式...
maven-java-formatter-plugin-0.3.jar
maven-java-formatter-plugin-0.4.jar
在Java编程语言中,`PrintStream`, `StringBuilder` 和 `Formatter` 是三个非常重要的类,分别用于不同的输出处理。理解并熟练使用这三个类是提升Java编程能力的关键。 首先,我们来详细了解一下`PrintStream`。它...
maven-java-formatter-plugin-0.4-sources.jar
HelloWorldApp.java 第一个用Java开发的应用程序。 firstApplet.java 第一个用Java开发的Applet小程序。 firstApplet.htm 用来装载Applet的网页文件 第2章 示例描述:本章介绍开发Java的基础语法知识。 ...
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
导入方法通常是:在Eclipse的`Window`菜单中选择`Preferences`,然后找到`Java` -> `Code Style` -> `Formatter`,点击`Import`按钮导入`JavaFormatter.xml`。 `使用说明.txt`文件则详细介绍了如何使用这个自定义的...
在Java编程语言中,控制输出格式是通过使用`java.text`包中的`NumberFormat`类实现的。这个类提供了一组方法,使得我们能够格式化数字、货币值和百分比,以便它们按照特定的样式和区域设置显示。下面将详细讨论这些...
常见的Java代码格式化工具有Eclipse的`Source`菜单、IntelliJ IDEA的`Reformat Code`功能,以及命令行工具如Google的`Java Formatter`。 在实际开发中,良好的注释习惯和有效的格式化工具结合使用,不仅可以提升...
Java类Formatter解析 Formatter是Java中的一个强大类库,它提供了强大且灵活的格式化功能,主要用于文本输出方面,例如数字、日期、金额等。以下是Formatter类的知识点总结: 1. Formatter类的功能:Formatter类...
此外,有一些编辑器插件如VS Code的`Java Formatter`,Sublime Text的`Java Format`等,也能方便地进行代码格式化。 6. **代码重构**:格式化代码不仅是美化,也是重构的一部分。通过统一的格式,可以更容易地发现...
- 使用代码格式化工具(如Google Java Formatter)保持代码风格一致。 通过自动生成实体类,开发者可以专注于业务逻辑,而不是重复的代码编写,从而提高生产力,降低错误率。对于大型项目,这种自动化实践更是不可...
Eclipse社区还开发了许多第三方代码格式化插件,如"Google Java Formatter",提供了更严格的代码风格,可以替代Eclipse默认的代码格式化工具。安装插件后,可以根据插件提供的设置进行配置。 总之,Eclipse Java ...