天天看點

16位流應用與代碼統計器例題

16位流應用:

使用PrintStream進行列印并且輸出到控制台以及文本上,每次的輸出要帶有一個格式為:yyyy-MM-dd  HH:mm:ss 的時間。

首先寫一個類繼承于PrintStream,然後重寫print和println方法,再替換一下系統的out輸出通道。

代碼示例:

<a href="https://s2.51cto.com/oss/201710/27/bbf0524b7d59eccbb889322617b90386.png" target="_blank"></a>

<a href="https://s5.51cto.com/oss/201710/27/1d5f4b43433c866b1e5abd1aecba4590.png" target="_blank"></a>

<a href="https://s2.51cto.com/oss/201710/27/ce3c0320053442683ed344514df1b3a8.png" target="_blank"></a>

接着寫一個測試類檢視實作效果:

<a href="https://s2.51cto.com/oss/201710/27/d6122dd04f0f77b771f0a250f1742e33.png" target="_blank"></a>

運作效果:

控制台:

<a href="https://s3.51cto.com/oss/201710/27/a1fce44849aef7ee4af8cf96904c1041.png" target="_blank"></a>

文本:

<a href="https://s1.51cto.com/oss/201710/27/1458a7ddf8cc95b8c12ac54782aa1e0b.png" target="_blank"></a>

FileReader/FileWriter流:

FileReader是16位流的檔案讀取流,裡面的方法和8位流基本是一樣的,一樣的讀取方法,有單個字元讀取,批量讀取和控制範圍讀取,skip跳過字元方法也有,其實在16流裡就是把概念從位元組換成了字元而已。

<a href="https://s2.51cto.com/oss/201710/27/edca1ce1a8a0905c5a19365fd5e6e1c2.png" target="_blank"></a>

運作結果:

<a href="https://s1.51cto.com/oss/201710/27/812b33ca144c7e65995ded0e4067c789.png" target="_blank"></a>

FileWriter是16位流裡的輸出流,裡面的方法也是和8位流基本是一樣的,也是有單個輸出,批量和控制範圍輸出。是以基本16位流也不需要過多贅述,隻要8位流掌握了,16位流隻要寫幾遍基本也就掌握了。

<a href="https://s1.51cto.com/oss/201710/27/55cabcf855fcb070b9f38ffc417a0d85.png" target="_blank"></a>

<a href="https://s4.51cto.com/oss/201710/27/9c7ab79caee791dc3edb8d6520f6bd45.png" target="_blank"></a>

BufferedReader/BufferedWriter流:

一目了然的緩存流,緩存池也是預設為8k,也可以在構造器中更改緩存的大小。16位流的緩存流倒是有個亮點,在16位流裡的讀取緩存流有個readLine方法可以一行行的讀取,讀到沒有值就會傳回一個null。

<a href="https://s4.51cto.com/oss/201710/27/d1470aff2b2c6592ee6d273f2f4d4045.png" target="_blank"></a>

<a href="https://s5.51cto.com/oss/201710/27/1ad1c356bd2e358699a9e87b5b96d4e2.png" target="_blank"></a>

InputStreamReader/OutputStreamWriter流:

InputStreamReader流可以将8位流轉換成16位流讀取,并且可以在構造器裡更改字元集,例如可以更改成UTF-8、GB2312等字元集,但是需要注意的是設定的字元集要與文本裡的字元集一緻,不然讀取出來的就是亂碼一般的文字。

OutputStreamWriter流可以将16位流轉換成8位流輸出,同樣的可以在構造器中更改字元集,同樣要注意的是如果你輸出的文本不支援你設定的字元集的話,輸出到文本上的内容也是亂碼一般的字元。

<a href="https://s5.51cto.com/oss/201710/27/4ed029e03896fbd98c1fc1fedc20eadc.png" target="_blank"></a>

運作結果:因為Eclipse的文本預設是GBK字元集,是以顯示不會出現問題:

<a href="https://s5.51cto.com/oss/201710/27/c4f6e0706fd149707136cdbaa97e60bf.png" target="_blank"></a>

制作一個代碼統計器:

要完成這個代碼統計器需要三個類:1.統計代碼行數類 2.搜尋字尾為.java檔案的搜尋類  3.測試類,首先完成統計代碼行數類,然後是檔案搜尋類,最後是實作類。

1. 統計代碼行數類:

<a href="https://s3.51cto.com/oss/201710/27/458b87c9e1b2eda8420e85d0b1f27b37.png" target="_blank"></a>

<a href="https://s3.51cto.com/oss/201710/27/a5a107a43d1590d865eb7680c1d704df.png" target="_blank"></a>

2.搜尋字尾為.java檔案的搜尋類:

  這個類使用遞歸查找就可以完成:

<a href="https://s1.51cto.com/oss/201710/27/8163b098ba7a1d39c0effdecd8ce38d0.png" target="_blank"></a>

3.測試類:

測試類基本就是最簡單了,調用完搜尋類的方法後就列印countLine和fileCount屬性就可以了:

<a href="https://s2.51cto.com/oss/201710/27/720fa4f8bfc69963df46e814bc3b43e3.png" target="_blank"></a>

<a href="https://s2.51cto.com/oss/201710/27/940a0c8d38274702e2d2231c7b69172a.png" target="_blank"></a>

本文轉自 ZeroOne01 51CTO部落格,原文連結:http://blog.51cto.com/zero01/1976629,如需轉載請自行聯系原作者