日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

输出java_java基础----Java的格式化输出

發布時間:2025/3/15 java 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 输出java_java基础----Java的格式化输出 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在JavaSe5中,推出了C語言中printf()風格的格式化輸出。這不僅使得控制輸出的代碼更加簡單,同時也給與Java開發者對于輸出格式與排列更大的控制能力。今天,我們開始學習Java中的格式化輸出。

目錄導航

System.out.format()

由于內容比較簡單,我們通過實例來加以說明。項目結構如下:

Java Se5引入的format方法可用于PrintStream或PrintWriter對象,其中也包括System.out對象。

packagecom.tomhu.format;public classFormatTest1 {public static voidmain(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.141592x= 5, y = 3.141592x= 5, y = 3.141592

可以看到,format與printf是等價的,它們只需要一個簡單的格式化字符串,加上一串參數即可,每個參數對應一個格式修飾符。

publicPrintStream printf(String format, Object ... args) {returnformat(format, args);

}

在format的具體代碼中,其實就是調用Formatter的format方法:formatter.format(Locale.getDefault(), format, args);

publicPrintStream 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對象的時候 ,需要向其構造器傳遞一些信息,告訴它最終的結果將向哪里輸出

packagecom.tomhu.format;importjava.util.Formatter;public classFormatTest2 {public static voidmain(String[] args) {

String name= "huhx";int age = 22;

Formatter formatter= newFormatter(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]conversion

The 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 conversionsfor 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,則會觸發異常

packagecom.tomhu.format;importjava.util.Formatter;public classFormatTest3 {static Formatter formatter = newFormatter(System.out);public static voidprintTitle() {

formatter.format("%-15s %-5s %-10s\n", "huhx", "linux", "liuli");

formatter.format("%-15s %-5s %-10s\n", "zhangkun", "yanzi", "zhangcong");

formatter.format("%-15s %-5s %-10s\n", "zhangkun", "yanzhou", "zhangcong");

}public static voidprint() {

formatter.format("%-15s %5d %10.2f\n", "My name is huhx", 5, 4.2);

formatter.format("%-15.4s %5d %10.2f\n", "My name is huhx", 5, 4.1);

}public static voidmain(String[] args) {

printTitle();

System.out.println("----------------------------");

print();

formatter.close();

}

}

它的輸出結果如下:

huhx linux liuli

zhangkun yanzi zhangcong

zhangkun yanzhou zhangcong----------------------------My name is huhx5 4.20My n5 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()還是很方便的。

packagecom.tomhu.format;public classFormatTest4 {public static voidmain(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 staticString format(String format, Object... args) {return newFormatter().format(format, args).toString();

}

簡單的十六進制轉換工具

packagecom.tomhu.format;public classFormatTest5 {public static String format(byte[] data) {

StringBuilder builder= newStringBuilder();int n = 0;for(byteb: 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");returnbuilder.toString();

}public static voidmain(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 782c00010: 20 77 65 6c 63 6f 6d 65 20 74 6f 20 6d 79 20 62

00020: 6c 6f 67

友情鏈接

總結

以上是生活随笔為你收集整理的输出java_java基础----Java的格式化输出的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。