Java之美[從菜鳥到高手演變]之Java中的IO

來源:互聯網
上載者:User

深入分析Java I/O 工作機制

作者:egg

郵箱:xtfggef@gmail.com

微博:http://weibo.com/xtfggef

部落格:http://blog.csdn.net/zhangerqing

論壇:http://www.qtlife.net(隨便建了一個,方便大家一起交流!)

IO是Java及眾多程式設計語言很重要的一塊,同時很多程式的瓶頸和耗時操作也都在IO這塊。所以能夠很好的解決IO問題對提高程式效能有很大的協助!本章我們將要系統的對Java IO做個分析,通過理論加實踐,希望讀者朋友們能真正徹底的理解並且掌握了它。本章系Java之美[從菜鳥到高手演變]系列之Java IO,通過本章的學習,讀者朋友們能基本瞭解到關於IO的很多知識。日後加以理解、分析、在項目中實踐,定能靈活運用!

一、簡介

IO操作面臨很多問題,資訊量的巨大,網路的環境等等,因為IO不僅僅是對本地檔案、目錄的操作,有時對二進位流、還有一部分是網路方面的資源,所以多種原因直接造成IO操作無疑是耗時且複雜多變的。Java對IO的支援是個不斷的演變過程,經過了很多的最佳化,直到JDK1.4以後,才趨於穩定,在JDK1.4中,加入了nio類,解決了很多效能問題,雖然我們有足夠的理由不去瞭解關於Java IO以前的情況,但是為了學好現在的類,我們還是打算去研究下,通過掌握類的最佳化情況來徹底理解IO的機制!Java IO主要主要在java.io包下,分為四大塊近80個類:

1、基於位元組操作的I/O介面:InputStream和OutputStream

2、基於字元操作的I/O介面:Writer和Reader

3、基於磁碟操作的I/O介面:File

4、基於網路操作的I/O介面:Socket(不在java.io包下)

影響IO效能的無非就是兩大因素:資料的格式及儲存的方式,前兩類主要是資料格式方面的,後兩個類是儲存方式方面的:本地和網路。所以策劃好這兩個方面的活動,有助於我們合理使用IO。

二、基於位元組的I/O操作(InputStream和OutputStream)

我們先來看看類圖:

圖1

圖2

二者類似,我只詳細講解InputStream類,OutputStream留給大家自己去學習。InputStream類是個抽象類別,裡面核心的方法就是read()、read(byte b[])、read(byte b[], int off, int len),這三個方法是用於讀取資料的底層的方法,他們可以用來讀取一下這些類型的資料:

A. 位元組數組

B. String對象

C. 檔案

D. 管道,從一端進入,從另一端輸出

E. 流

F. internet資源

每一種資料來源都有相應的InputStream子類,因為InputStream是個處於頂層的類,用來處理各種資料來源的類都繼承了InputStream類,我們來看看這些類:

ByteArrayInputStream:處理位元組數組的類,允許將記憶體的緩衝區當做InputStream使用。

StringBufferInputStream:將String轉換成InputStream,內部實現用的是StringBuffer。

FileInputStream:從檔案中讀取資料。

PipedInputStream:用於從管道中讀取資料。

SequenceInputStream:將多個流對象轉化成一個InputStream。

FilterInputStream:裝飾器類,為其它InputStream類提供功能。

做過關於IO操作的讀者知道,我們很少單獨使用哪個類來實現IO操作,平時都是幾個類合起來使用,這其實體現了一種裝飾器模式(詳見:http://blog.csdn.net/zhangerqing)的思想,在後面的分析中我們會詳細的分析。從上面的圖1中我們可以看出,FilterInputStream雖說是Inputstream的子類,但它依然是BufferedInputStream、DataInputStream、LineNumberInputStream、PushbackInputStream類的父類,這四個類分別提供了最貼近我們程式員使用的方法,如:readInt() 、readInt()、readInt()等等。對於IO操作,不管是磁碟還是網路,最終都是對位元組的操作,而我們平時寫的程式都是字元形式的,所以在傳輸的過程中需要進行轉換。在字元到位元組的轉換過程中,我們需要用到一個類:InputStreamReader。

三、基於字元的I/O操作(Writer和Reader)

圖3

圖4

Writer和Reader操作的目的就是操作字元和不是位元組,和InputStream和OutputStream配合增加IO效果。通過InputStreamReader和OutputStreamReader可以進行位元組和字元的轉換,設計Writer和Reader的目的是國際化,使IO操作支援16位的Unicode。我把它們單獨的畫出來,因為要是全畫的話,太大了放不下,有興趣的TX可以在rational rose中匯入其帶的JDK類圖看看,很過癮的!

四、基於磁碟的I/O操作(File)

五、基於網路的I/O操作(Socket)

六、NIO

四-六部分由於時間關係,還沒有整理完,後續會補出來!

七、經典IO操作

1、緩衝輸入檔案。

import java.io.BufferedReader;import java.io.FileReader;public class InputStreamTest {public static String read(String filename) throws Exception {BufferedReader br = new BufferedReader(new FileReader(filename));String s;StringBuffer sb = new StringBuffer();while ((s = br.readLine()) != null) {sb.append(s + "\n");}br.close();return sb.toString();}public static void main(String[] args) throws Exception {System.out.println(read("src/InputStreamTest.java"));} }

這段代碼是從磁碟讀入InputStreamTest.java檔案,然後轉換成字串。輸出就是將源檔案原樣輸出。

2、從記憶體中讀取。

import java.io.StringReader;public class MemoryInput {public static void main(String[] args) throws Exception {StringReader in = new StringReader(InputStreamTest.read("src/MemoryInput.java"));int c;while ((c = in.read()) != -1)System.out.println((char) c);}}

read返回的是int類型的資料,所以在輸出語句中用char做了強型別轉換。該程式將一個一個的輸出字元。

3、基本的檔案輸出。

import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.PrintWriter;import java.io.StringReader;public class BasicFileOutput {static String file = "basie.out";public static void main(String[] args) throws Exception {BufferedReader in = new BufferedReader(new StringReader(InputStreamTest.read("src/BasicFileOutput.java")));PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));int lineCount = 1;String s;while ((s = in.readLine()) != null) {out.println(lineCount++ + ": " + s);}out.close();System.out.println(InputStreamTest.read(file));}}

輸出:

1: import java.io.BufferedReader;

2: import java.io.BufferedWriter;

3: import java.io.FileWriter;

…

 4、RandomAccessFile

RandomAccessFile被我們稱為”自我獨立的類”,因為它獨立於我們前面說的IO類,與InputStream和OutputStream沒什麼關係,除了實現了DataOutput, DataInput兩個介面外。所有方法都是重新編寫,而且很多都是native方法,我們來看個例子,瞭解下這個類:

5、管道流

八、標準I/O

就是我們最原始的使用的從控制台輸入或者輸出的那些類和方法,如System.in、System.out等。

public class StandardIO {public static void main(String[] args) throws IOException {BufferedReader in = new BufferedReader(new InputStreamReader(System.in));String s;while ((s = in.readLine()) != null && s.length() != 0)System.out.println(s);}}

System.in返回的是未經封裝的InputStream對象,所以需要進行裝飾,經InputStreamReader轉換為Reader對象,放入BufferedReader的構造方法中。除此之外,System.out和System.err都是直接的PriintStream對象,可直接使用。我們也可以使用java.util包下的Scanner類來代替上述程式:

public class StandardIO {public static void main(String[] args) throws IOException {Scanner in = new Scanner(System.in);String s;while((s = in.next()) != null && s.length() != 0){System.out.println(s);}}}

九、效能分析及總結

1、一個檔案讀寫工具類。

import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Arrays;/** * 一個非常實用的檔案操作類 . 2012-12-19 *  * @author Bruce Eckel , edited by erqing *  */public class TextFile extends ArrayList<String> {private static final long serialVersionUID = -1942855619975438512L;// Read a file as a Stringpublic static String read(String filename) {StringBuilder sb = new StringBuilder();try {BufferedReader in = new BufferedReader(new FileReader(new File(filename).getAbsoluteFile()));String s;try {while ((s = in.readLine()) != null) {sb.append(s);sb.append("\n");}} finally {in.close();}} catch (IOException e) {throw new RuntimeException(e);}return sb.toString();}// Write a single file in one method callpublic static void write(String fileName, String text) {try {PrintWriter out = new PrintWriter(new File(fileName).getAbsoluteFile());try {out.print(text);} finally {out.close();}} catch (IOException e) {throw new RuntimeException(e);}}// Read a file,spilt by any regular expressionpublic TextFile(String fileName, String splitter) {super(Arrays.asList(read(fileName).split(splitter)));if (get(0).equals(""))remove(0);}// Normally read by linespublic TextFile(String fileName) {this(fileName, "\n");}public void write(String fileName) {try {PrintWriter out = new PrintWriter(new File(fileName).getAbsoluteFile());try {for (String item : this)out.println(item);} finally {out.close();}} catch (IOException e) {throw new RuntimeException(e);}}// test,I have generated a file named data.d at the rootpublic static void main(String[] args) {/* read() test */System.out.println(read("data.d")); // testing is OK!/* write() test */write("out.d", "helloworld\negg"); // testing is OK!/* constractor test */TextFile tf = new TextFile("data.d"); // testing is OK!}}

2、讀取二進位檔案。

import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;/** * to read the binary file *  * @author erqing *  */public class BinaryFile {/* the parametre is a file */public static byte[] read(File file) throws IOException {BufferedInputStream bf = new BufferedInputStream(new FileInputStream(file));try {byte[] data = new byte[bf.available()];bf.read(data);return data;} finally {bf.close();}}/* the param is the path of a file */public static byte[] read(String file) throws IOException {return read(new File(file).getAbsoluteFile());}}

本章節未完,因為最近較忙,所以先發出一部分,後續會將缺少的補齊!

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.