java基础----Java的格式化输出
在JavaSe5中,推出了C語言中printf()風格的格式化輸出。這不僅使得控制輸出的代碼更加簡單,同時也給與Java開發者對于輸出格式與排列更大的控制能力。今天,我們開始學習Java中的格式化輸出。
?
目錄導航
?
System.out.format()
由于內容比較簡單,我們通過實例來加以說明。項目結構如下:
Java Se5引入的format方法可用于PrintStream或PrintWriter對象,其中也包括System.out對象。
package com.tomhu.format;public class FormatTest1 {public static void main(String[] args) {int x = 5;double y = 3.141592;// 一般方式System.out.println("x = " + x + ", y = " + y);// printf()方式System.out.printf("x = %d, y = %f\n", x, y);// format()方式System.out.format("x = %d, y = %f\n", x, y);} }輸出的結果如下:
x = 5, y = 3.141592 x = 5, y = 3.141592 x = 5, y = 3.141592可以看到,format與printf是等價的,它們只需要一個簡單的格式化字符串,加上一串參數即可,每個參數對應一個格式修飾符。
public PrintStream printf(String format, Object ... args) {return format(format, args); }?在format的具體代碼中,其實就是調用Formatter的format方法:formatter.format(Locale.getDefault(), format, args);
public PrintStream format(String format, Object ... args) {try {synchronized (this) {ensureOpen();if ((formatter == null)|| (formatter.locale() != Locale.getDefault()))formatter = new Formatter((Appendable) this);formatter.format(Locale.getDefault(), format, args);}} catch (InterruptedIOException x) {Thread.currentThread().interrupt();} catch (IOException x) {trouble = true;}return this; }?
Formatter類
在Java中,所有新的格式化功能都由Formatter類處理,上述的printf與format也是。可以將Formatter看作是一個翻譯器,它將你的格式化字符串與數據翻譯成需要的結果。當你創建一個Formatter對象的時候 ,需要向其構造器傳遞一些信息,告訴它最終的結果將向哪里輸出
package com.tomhu.format;import java.util.Formatter;public class FormatTest2 {public static void main(String[] args) {String name = "huhx";int age = 22;Formatter formatter = new Formatter(System.out);formatter.format("My name is %s, and my age is %d ", name, age);formatter.close();} }它的輸出結果如下:
My name is huhx, and my age is 22?
格式化說明符
在插入數據時,如果想要控制空格與對齊,就需要精細復雜的格式修飾符,以下是其抽象的語法:
%[argument_index$][flags][width][.precision]conversionThe optional argument_index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$", the second by "2$", etc.The optional flags is a set of characters that modify the output format. The set of valid flags depends on the conversion.The optional width is a non-negative decimal integer indicating the minimum number of characters to be written to the output.The optional precision is a non-negative decimal integer usually used to restrict the number of characters. The specific behavior depends on the conversion.The required conversion is a character indicating how the argument should be formatted. The set of valid conversions for a given argument depends on the argument's data type.?最常見的應用是控制一個域的最小尺寸,這可以通過指定width來實現。Formatter對象通過在必要時添加空格,來確保一個域至少達到某個長度。在默認的情況下,數據是右對齊的,通過"-"標志可以改變對齊的方向。
與width相對的是precision(精確度),它用來指明最大尺寸。width可以應用各種類型的數據轉換,并且其行為方式都一樣。precision則不一樣,不是所有類型的數據都能使用precision,而且,應用于不同的類型的數據轉換時,precision的意義也不同。
- precision應用于String時,它表示打印String時輸出字符的最大數量
- precision應用于浮點數時,它表示小數點要顯示出來的位數。默認是6位小數,如果小數位數過多則舍入,過少則在尾部補零。
- 由于整數沒有小數部分,所以precision不能應用于整數。如果你對整數應用precision,則會觸發異常
它的輸出結果如下:
huhx linux liuli zhangkun yanzi zhangcong zhangkun yanzhou zhangcong ---------------------------- My name is huhx 5 4.20 My n 5 4.10?
Formatter轉換
?下面的表格包含了最常用的類型轉換:
| d | 整數型(10進制 ) | e | 浮點數(科學計數) |
| c | Unicode字符 | x | 整數(16進制) |
| b | Boolean值 | h | 散列碼(16進制) |
| s | String | % | 字符"%" |
| f | 浮點數(10進制) | ? | ? |
String.format()是一個static方法,它接受與Formatter.format()方法一樣的參數,但返回一個String對象。當你只需要用format方法一次的時候,String.format()還是很方便的。
package com.tomhu.format;public class FormatTest4 {public static void main(String[] args) {int age = 22;String name = "huhx";String info = String.format("My name is %s and my age is %d", name, age);System.out.println(info);} }它的輸出結果如下:
My name is huhx and my age is 22其實String.format方法的實質還是Formatter.format(),只不過是做了簡單封裝而已:
public static String format(String format, Object... args) {return new Formatter().format(format, args).toString(); }?
簡單的十六進制轉換工具
package com.tomhu.format;public class FormatTest5 {public static String format(byte[] data) {StringBuilder builder = new StringBuilder();int n = 0;for(byte b: data) {if (n %16 == 0) {builder.append(String.format("%05x: ", n));}builder.append(String.format("%02x ", b));n ++;if (n % 16 == 0) {builder.append("\n");}}builder.append("\n");return builder.toString();}public static void main(String[] args) {String string = "my name is huhx, welcome to my blog";System.out.println(format(string.getBytes()));} }輸出結果如下:
00000: 6d 79 20 6e 61 6d 65 20 69 73 20 68 75 68 78 2c 00010: 20 77 65 6c 63 6f 6d 65 20 74 6f 20 6d 79 20 62 00020: 6c 6f 67 作者:?huhx?出處:?www.cnblogs.com/huhx
總結
以上是生活随笔為你收集整理的java基础----Java的格式化输出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java基础----Base64算法的使
- 下一篇: java高级----Java动态代理的原