lyyb2001

          只是為方便自己找記錄而已
          posts - 57, comments - 27, trackbacks - 0, articles - 5
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 ::  :: 管理

          Java的IO類結(jié)構(gòu):
          ????? 根接口是InputStream/OutputStream,充當數(shù)據(jù)源的IO類有FileInputStream/FileOutputStream,ByteArrayInputStream? / ByteArrayOutputStream? 等,充當裝飾功能的IO類有BufferedInputStream? /?? BufferedOutputStream,DataInputStream?? /?? DataOutputStream等,
          ?? 它們都是繼承裝飾接口FilterInputStream/FilterOutputStream。
          ????? 使用IO時,首先創(chuàng)建一個數(shù)據(jù)源IO,然后根據(jù)需要的功能創(chuàng)建裝飾類IO,其構(gòu)造函數(shù)的參數(shù)為已創(chuàng)建的數(shù)據(jù)源IO。
          ????? 我們以創(chuàng)建一個具有緩沖的文件輸入流為例,假定需要從磁盤讀取文件“C:\log.txt”:
          ????? // 創(chuàng)建一個FileInputStream:
          ????? FileInputStream fileInput = new FileInputStream(”C:\\log.txt”);
          ????? // 創(chuàng)建一個BufferedInputStream:
          ????? BufferedInputStream bufferedInput = new BufferedInputStream(fileInput);
          ???? // 現(xiàn)在得到的bufferedInput即是具有緩沖的文件輸入流
            或者進一步簡寫如下:
          ???? InputStream input = new BufferedInputStream(new FileInputStream(”C:\\log.txt”));
          ???? // 現(xiàn)在得到的input即是具有緩沖的文件輸入流
          java.io.Reader 和 java.io.InputStream 區(qū)別
            java.io.Reader 和 java.io.InputStream 組成了 Java 輸入類。Reader 用于讀入16位字符,也就是 Unicode 編碼的字符;而 InputStream 用于讀入 ASCII 字符和二進制數(shù)據(jù)。
          在 Java 中,有不同類型的 Reader 輸入流對應(yīng)于不同的數(shù)據(jù)源:
          ??? FileReader 用于從文件輸入;
          ??? CharArrayReader 用于從程序中的字符數(shù)組輸入;
          ??? StringReader 用于從程序中的字符串輸入;
          ??? PipedReader 用于讀取從另一個線程中的 PipedWriter 寫入管道的數(shù)據(jù)。
          相應(yīng)的也有不同類型的 InputStream 輸入流對應(yīng)于不同的數(shù)據(jù)源:FileInputStream,ByteArrayInputStream,StringBufferInputStream,PipedInputStream。另外,還有兩種沒有對應(yīng) Reader 類型的 InputStream 輸入流:
          ??? Socket 用于套接字;
          ??? URLConnection 用于 URL 連接。
          這兩個類使用 getInputStream() 來讀取數(shù)據(jù)。
          相應(yīng)的,java.io.Writer 和 java.io.OutputStream 也有類似的區(qū)別。
          1、Java技術(shù)支持兩種數(shù)據(jù)類型的流
          InputStream和OutputStream:字節(jié)流。其它字節(jié)流都是InputStream或OutputStream的子類。
          Reader和 Writer:字符流。其它字符流都是Reader或Writer的子類。
          2、節(jié)點流
          Java 2 SDK中有三種基本類型的節(jié)點:文件(file)、內(nèi)存(memory)、管道(pipe)。
          3、過程流
          過程流在其它流之上,完成排序、變換等操作。過程流也被稱做過濾流。
          當你需要改變輸入流的原始數(shù)據(jù)時,你可以將一個過濾輸入流連接到一個原始的輸入流上。
          用過濾流將原始數(shù)據(jù)變換成你需要的格式。?
          4、基本字節(jié)流類
          4.1、FileInputStream和FileOutputStream
          這兩個節(jié)點流用來操縱磁盤文件。這些類的構(gòu)造函數(shù)允許你指定它們所連接的文件。
          要構(gòu)造一個FileInputStream,所關(guān)聯(lián)的文件必須存在而且是可讀的。
          如果你要構(gòu)造一個FileOutputStream而輸出文件已經(jīng)存在,則它將被覆蓋。
          FileInputStream infile = new FileInputStream(”myfile.dat”);
          FileOutputStream outfile = new FileOutputStream(”results.dat”);
          4.1、 BufferInputStream和BufferOutputStream
          這些是過濾器流,它們可以提高I/O操作的效率。
          4.3、 PipedInputStream和PipedOutputStream
          管道流用來在線程間進行通信。一個線程的PipedInputStream對象從另一個線程的PipedOutputStream對象讀取輸入。
          要使管道流有用,必須有一個輸入方和一個輸出方。
          4.4、 DataInputStream和DataOutputStream
          這些過濾器通過流來讀寫Java基本類
          5、 基本字符流類
          圖闡述了Reader和Writer字符流的體系結(jié)構(gòu)。
          5.1、InputStreamReader 和 OutputStreamWriter
          用于字節(jié)流與字符流之間的轉(zhuǎn)換接口。
          當你構(gòu)造一個InputStreamReader或OutputStreamWriter時,轉(zhuǎn)換規(guī)則定義了16位Unicode和其它平臺的特定表示之間的轉(zhuǎn)換。
          InputStreamReader從一個數(shù)據(jù)源讀取字節(jié),并自動將其轉(zhuǎn)換成Unicode字符。
          如果你特別聲明,InputStreamReade會將字節(jié)流轉(zhuǎn)換成其它種類的字符流。
          OutputStreamWriter將字符的Unicode編碼寫到輸出流,如果你的使用的不是Unicode字符,OutputStreamWriter會將你的字符編碼轉(zhuǎn)換成Unicode編碼。
          5.2.、緩沖讀者和作者
          因為在各種格式之間進行轉(zhuǎn)換和其它I/O操作很類似,所以在處理大塊數(shù)據(jù)時效率最高。
          在InputStreamReader和OutputStreamWriter的結(jié)尾鏈接一個BufferedReader和BufferedWriter是一個好主意。
          記住對BufferedWriter使用flush()方法。
          5.3、 使用其它字符轉(zhuǎn)換
          如果你需要從一個非本地(例如,從連接到一個不同類型的機器的網(wǎng)絡(luò)連接讀取)的字符編碼讀取輸入,
          你可以象下面這個程序那樣,使用顯式的字符編碼構(gòu)造ir=new InputStreamReader(System.in,? “8859_1″);
          注:如果你通過網(wǎng)絡(luò)連接讀取字符,就應(yīng)該使用這種形式。
          否則,你的程序會總是試圖將所讀取的字符當作本地表示來進行轉(zhuǎn)換,而這并不總是正確的。ISO 8859-1是映射到ASCII的Latin-1編碼模式。
          6、 對象串行化
          java.io.Serializable接口支持將一個Java技術(shù)對象存放到一個流中。
          將一個對象存放到某種類型的永久存儲器上稱為”保持”。
          如果一個對象可以被存放到磁盤或磁帶上,或者可以發(fā)送到另外一臺機器并存放到存儲器或磁盤上,那么這個對象就被稱為可保持的。
          java.io.Serializable接口沒有任何方法,它只作為一個”標記”,用來表明實現(xiàn)了這個接口的類可以串行化。
          類中沒有實現(xiàn)Serializable接口的對象不能被保持。
          // 文件實現(xiàn)追加:
          // 其中的FileWriter()中的第二個參數(shù)的含義是:是否在文件中追加內(nèi)容
          PrintWriter out = new PrintWriter(new FileWriter(logFileName, true), true);
          Java讀寫文件最常用的類是FileInputStream/FileOutputStream和FileReader/FileWriter。
          其中FileInputStream和FileOutputStream是基于字節(jié)流的,常用于讀寫二進制文件。
          讀寫字符文件建議使用基于字符的FileReader和FileWriter,省去了字節(jié)與字符之間的轉(zhuǎn)換。
          但這兩個類的構(gòu)造函數(shù)默認使用系統(tǒng)的編碼方式,如果文件內(nèi)容與系統(tǒng)編碼方式不一致,可能會出現(xiàn)亂碼。
          在這種情況下,建議使用FileReader和FileWriter的父類:InputStreamReader/OutputStreamWriter,
          它們也是基于字符的,但在構(gòu)造函數(shù)中可以指定編碼類型:InputStreamReader(InputStream in, Charset cs) 和OutputStreamWriter(OutputStream out, Charset cs)。

          // 讀寫文件的編碼:
          InputStreamReader r = new InputStreamReader(new FileInputStream(fileName), “utf-8″);
          OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fileName),”utf-8″);

          /**
          三種IO性能比較:
          在讀寫一個10k文件的時候,三種方式的耗時如下:
          InputStreamReader And OutputStreamWriter : 63ms (可以設(shè)置文件的編碼,如果不用buffer)
          BufferedReader And BufferedWriter : 31ms
          BufferedInputStream And BufferedOutputStream : 16ms
          */

          /**
          * Description: Test the java IO’s efficiency
          * Author: KevinChen
          * Date: 2007-02-18
          */
          import java.io.*;

          /**
          * using the InputStreamReader And OutputStreamWriter
          */
          class EncoderRW {
          ?public static String read(String fileName) throws IOException {
          ? StringBuffer sb = new StringBuffer();
          ? /*此處讀文件時用了buffer,如果不用,性能損失一倍*/
          ? BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), “utf-8″));
          ? String s;
          ? while((s = in.readLine()) != null) {
          ??? sb.append(s);
          ??? sb.append(”\n”);
          ? }
          ? in.close();
          ? return sb.toString();
          ?}
          ?public void write(String fileName, String text) throws IOException {
          ? OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fileName),”utf-8″);
          ? out.write(text);
          ? out.flush();
          ? out.close();
          ?}
          }

          /**
          * using the BufferedReader And BufferedWriter
          */
          class WriterReader {
          ?public String read(String fileName) throws IOException {
          ? StringBuffer sb = new StringBuffer();
          ? BufferedReader in = new BufferedReader(new FileReader(fileName));
          ? String s;
          ? while((s = in.readLine()) != null) {
          ??? sb.append(s);
          ??? sb.append(”\n”);
          ? }
          ? in.close();
          ? return sb.toString();
          ?}
          ?public void write(String fileName, String text) throws IOException {
          ? PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
          ? out.print(text);
          ? out.close();
          ?}
          }

          /**
          * using the BufferedInputStream And BufferedOutputStream
          */
          class BufferedStream{
          ?public byte[] read(String fileName) throws IOException {
          ? BufferedInputStream remoteBIS = new BufferedInputStream(new FileInputStream(fileName));
          ? ByteArrayOutputStream baos = new ByteArrayOutputStream(10240);
          ? byte[] buf = new byte[1024];
          ? int bytesRead = 0;
          ? while(bytesRead >= 0)
          ? {
          ?? baos.write(buf, 0, bytesRead);
          ?? bytesRead = remoteBIS.read(buf);
          ? }
          ? byte[] content = baos.toByteArray();
          ? return content;
          ?}
          ?public void write(String fileName, byte[] content)? throws IOException {
          ? BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
          ? out.write(content);
          ? out.flush();
          ? out.close();
          ?}
          }

          public class TestIO
          {
          ?public static void main(String[] args)throws IOException {
          ? long currentTime = System.currentTimeMillis() ;
          ? EncoderRW rw = new EncoderRW();
          ? rw.write(”index.dat”,rw.read(”FileUtil.java”));
          ? System.out.println(”cost time:” + Long.toString(System.currentTimeMillis()-currentTime) + ” ms”);

          ? currentTime = System.currentTimeMillis() ;
          ? WriterReader wr = new WriterReader();
          ? wr.write(”index.dat”,wr.read(”FileUtil.java”));
          ? System.out.println(”cost time:” + Long.toString(System.currentTimeMillis()-currentTime) + ” ms”);

          ? currentTime = System.currentTimeMillis() ;
          ? BufferedStream bf = new BufferedStream();
          ? bf.write(”index.dat”,bf.read(”FileUtil.java”));
          ? System.out.println(”cost time:” + Long.toString(System.currentTimeMillis()-currentTime) + ” ms”);
          ?}
          }



          Lyyb2001

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 博兴县| 阳春市| 上犹县| 原平市| 灵武市| 丰镇市| 红安县| 息烽县| 柳江县| 微山县| 莱西市| 扎赉特旗| 文水县| 无极县| 河津市| 南部县| 兴安县| 嘉定区| 油尖旺区| 富顺县| 崇明县| 乐昌市| 安达市| 饶平县| 逊克县| 黄冈市| 贞丰县| 成安县| 双辽市| 德江县| 中西区| 清徐县| 涟水县| 巢湖市| 连云港市| 德清县| 盐城市| 永清县| 农安县| 灵寿县| 精河县|