夢想飛翔

          自強(qiáng)不息
          posts - 111, comments - 30, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Java語言深入 JAVA之精髓IO流

          Posted on 2009-04-29 14:25 love1563 閱讀(216) 評論(0)  編輯  收藏 所屬分類: Java語言*初級版

          一.input和output
          1.stream代表的是任何有能力產(chǎn)出數(shù)據(jù)的數(shù)據(jù)源,或是任何有能力接收數(shù)據(jù)的接收源。
          在java的io中,所有的stream(包括input和out stream)都包括兩種類型:
          1.1 以字節(jié)為導(dǎo)向的stream
          以字節(jié)為導(dǎo)向的stream,表示以字節(jié)為單位從stream中讀取或往stream中寫入信息。以字節(jié)為導(dǎo)向的stream包括下面幾種類型:
          1) input stream:
          1) bytearrayinputstream:把內(nèi)存中的一個緩沖區(qū)作為inputstream使用
          2) stringbufferinputstream:把一個string對象作為inputstream
          3) fileinputstream:把一個文件作為inputstream,實現(xiàn)對文件的讀取操作
          4) pipedinputstream:實現(xiàn)了pipe的概念,主要在線程中使用
          5) sequenceinputstream:把多個inputstream合并為一個inputstream
          2) out stream
          1) bytearrayoutputstream:把信息存入內(nèi)存中的一個緩沖區(qū)中
          2) fileoutputstream:把信息存入文件中
          3) pipedoutputstream:實現(xiàn)了pipe的概念,主要在線程中使用
          4) sequenceoutputstream:把多個outstream合并為一個outstream
          1.2 以unicode字符為導(dǎo)向的stream
          以unicode字符為導(dǎo)向的stream,表示以unicode字符為單位從stream中讀取或往stream中寫入信息。以unicode字符為導(dǎo)向的stream包括下面幾種類型:
          1) input stream
          1) chararrayreader:與bytearrayinputstream對應(yīng)
          2) stringreader:與stringbufferinputstream對應(yīng)
          3) filereader:與fileinputstream對應(yīng)[來源 www.iocblog.net]
          4) pipedreader:與pipedinputstream對應(yīng)
          2) out stream
          1) chararraywrite:與bytearrayoutputstream對應(yīng)
          2) stringwrite:無與之對應(yīng)的以字節(jié)為導(dǎo)向的stream
          3) filewrite:與fileoutputstream對應(yīng)
          4) pipedwrite:與pipedoutputstream對應(yīng)
          以字符為導(dǎo)向的stream基本上對有與之相對應(yīng)的以字節(jié)為導(dǎo)向的stream。兩個對應(yīng)類實現(xiàn)的功能相同,字是在操作時的導(dǎo)向不同。如chararrayreader:和bytearrayinputstream的作用都是把內(nèi)存中的一個緩沖區(qū)作為inputstream使用,所不同的是前者每次從內(nèi)存中讀取一個字節(jié)的信息,而后者每次從內(nèi)存中讀取一個字符。
          1.3 兩種不現(xiàn)導(dǎo)向的stream之間的轉(zhuǎn)換
          inputstreamreader和outputstreamreader:把一個以字節(jié)為導(dǎo)向的stream轉(zhuǎn)換成一個以字符為導(dǎo)向的stream。
          2. stream添加屬性
          2.1 “為stream添加屬性”的作用
          運(yùn)用上面介紹的java中操作io的api,我們就可完成我們想完成的任何操作了。但通過filterinputstream和filteroutstream的子類,我們可以為stream添加屬性。下面以一個
          例子來說明這種功能的作用。
          如果我們要往一個文件中寫入數(shù)據(jù),我們可以這樣操作:
          fileoutstream fs = new fileoutstream(“test.txt”);
          然后就可以通過產(chǎn)生的fs對象調(diào)用write()函數(shù)來往test.txt文件中寫入數(shù)據(jù)了。但是,如果我們想實現(xiàn)“先把要寫入文件的數(shù)據(jù)先緩存到內(nèi)存中,再把緩存中的數(shù)據(jù)寫入文件中”的功能時,上面的api就沒有一個能滿足我們的需求了。但是通過filterinputstream和filteroutstream的子類,為fileoutstream添加我們所需要的功能。
          2.2 filterinputstream的各種類型
          2.2.1 用于封裝以字節(jié)為導(dǎo)向的inputstream
          1) datainputstream:從stream中讀取基本類型(int、char等)數(shù)據(jù)。
          2) bufferedinputstream:使用緩沖區(qū)
          3) linenumberinputstream:會記錄input stream內(nèi)的行數(shù),然后可以調(diào)用getlinenumber()和setlinenumber(int)
          4) pushbackinputstream:很少用到,一般用于編譯器開發(fā)
          2.2.2 用于封裝以字符為導(dǎo)向的inputstream
          1) 沒有與datainputstream對應(yīng)的類。除非在要使用readline()時改用bufferedreader,否則使用datainputstream
          2) bufferedreader:與bufferedinputstream對應(yīng)
          3) linenumberreader:與linenumberinputstream對應(yīng)
          4) pushbackreader:與pushbackinputstream對應(yīng)
          2.3 filteroutstream的各種類型
          2.2.3 用于封裝以字節(jié)為導(dǎo)向的outputstream

          1) dataioutstream:往stream中輸出基本類型(int、char等)數(shù)據(jù)。
          2) bufferedoutstream:使用緩沖區(qū)
          3) printstream:產(chǎn)生格式化輸出
          2.2.4 用于封裝以字符為導(dǎo)向的outputstream
          1) bufferedwrite:與對應(yīng)
          2) printwrite:與對應(yīng)
          3. randomaccessfile
          1) 可通過randomaccessfile對象完成對文件的讀寫操作
          2) 在產(chǎn)生一個對象時,可指明要打開的文件的性質(zhì):r,只讀;w,只寫;rw可讀寫
          3) 可以直接跳到文件中指定的位置

          4. i/o應(yīng)用的一個例子
          import java.io.*;
          public class testio{
          public static void main(string[] args)
          throws ioexception{
          //1.以行為單位從一個文件讀取數(shù)據(jù)
          bufferedreader in = 
          new bufferedreader(
          new filereader("f:\nepalon\testio.java"));
          string s, s2 = new string();
          while((s = in.readline()) != null)
          s2 += s + " ";
          in.close();

          //1b. 接收鍵盤的輸入
          bufferedreader stdin = 
          new bufferedreader(
          new inputstreamreader(system.in));
          system.out.println("enter a line:");
          system.out.println(stdin.readline());

          //2. 從一個string對象中讀取數(shù)據(jù)
          stringreader in2 = new stringreader(s2);
          int c;
          while((c = in2.read()) != -1)
          system.out.println((char)c);
          in2.close();

          //3. 從內(nèi)存取出格式化輸入
          try{
          DataInputStream in3 =
          new DataInputStream(
          new ByteArrayInputStream(s2.getBytes()));
          while(true)
          System.out.println((char)in3.readByte());
          }
          catch(EOFException e){
          System.out.println("End of stream");
          }

           

          //4. 輸出到文件
          try{
          BufferedReader in4 = new BufferedReader(new StringReader(s2));
          PrintWriter out1 =
          new PrintWriter(
          new BufferedWriter(
          new FileWriter("F:\\nepalon\\ TestIO.out")));
          int lineCount = 1;
          while((s = in4.readLine()) != null)
          out1.println(lineCount++ + ":" + s);
          out1.close();
          in4.close();
          }
          catch(EOFException ex){
          System.out.println("End of stream");
          }

          //5. 數(shù)據(jù)的存儲和恢復(fù)
          try{
          DataOutputStream out2 =
          new DataOutputStream(
          new BufferedOutputStream(
          new FileOutputStream("F:\\nepalon\\ Data.txt")));
          out2.writeDouble(3.1415926);
          out2.writeChars("\nThas was pi:writeChars\n");
          out2.writeBytes("Thas was pi:writeByte\n");
          out2.close();
          DataInputStream in5 =
          new DataInputStream(
          new BufferedInputStream(
          new FileInputStream("F:\\nepalon\\ Data.txt")));
          BufferedReader in5br =
          new BufferedReader(
          new InputStreamReader(in5));
          System.out.println(in5.readDouble());
          System.out.println(in5br.readLine());
          System.out.println(in5br.readLine());
          }
          catch(EOFException e){
          System.out.println("End of stream");
          }

          //6. 通過RandomAccessFile操作文件
          RandomAccessFile rf =
          new RandomAccessFile("F:\\nepalon\\ rtest.dat", "rw");
          for(int i=0; i<10; i++)
          rf.writeDouble(i*1.414);
          rf.close();[來源 www.iocblog.net]

           

          rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "r");
          for(int i=0; i<10; i++)
          System.out.println("Value " + i + ":" + rf.readDouble());
          rf.close();

          rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "rw");
          rf.seek(5*8);
          rf.writeDouble(47.0001);
          rf.close();

          rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "r");
          for(int i=0; i<10; i++)
          System.out.println("Value " + i + ":" + rf.readDouble());
          rf.close();
          }
          }
          關(guān)于代碼的解釋(以區(qū)為單位):
          1區(qū)中,當(dāng)讀取文件時,先把文件內(nèi)容讀到緩存中,當(dāng)調(diào)用in.readLine()時,再從緩存中以字符的方式讀取數(shù)據(jù)(以下簡稱“緩存字節(jié)讀取方式”)。
          1b區(qū)中,由于想以緩存字節(jié)讀取方式從標(biāo)準(zhǔn)IO(鍵盤)中讀取數(shù)據(jù),所以要先把標(biāo)準(zhǔn)IO(System.in)轉(zhuǎn)換成字符導(dǎo)向的stream,再進(jìn)行BufferedReader封裝。
          2區(qū)中,要以字符的形式從一個String對象中讀取數(shù)據(jù),所以要產(chǎn)生一個StringReader類型的stream。
          4區(qū)中,對String對象s2讀取數(shù)據(jù)時,先把對象中的數(shù)據(jù)存入緩存中,再從緩沖中進(jìn)行讀取;對TestIO.out文件進(jìn)行操作時,先把格式化后的信息輸出到緩存中,再把緩存中的信息輸出到文件中。
          5 區(qū)中,對Data.txt文件進(jìn)行輸出時,是先把基本類型的數(shù)據(jù)輸出屋緩存中,再把緩存中的數(shù)據(jù)輸出到文件中;對文件進(jìn)行讀取操作時,先把文件中的數(shù)據(jù)讀取到緩存中,再從緩存中以基本類型的形式進(jìn)行讀取。注意in5.readDouble()這一行。因為寫入第一個writeDouble(),所以為了正確顯示。也要以基本類型的形式進(jìn)行讀取。
          6區(qū)是通過RandomAccessFile類對文件進(jìn)行操作。

          主站蜘蛛池模板: 冷水江市| 顺义区| 武邑县| 仁寿县| 奉节县| 揭东县| 浦县| 家居| 六安市| 乌兰县| 莆田市| 乳山市| 辉县市| 安多县| 石渠县| 清原| 庆城县| 小金县| 垫江县| 色达县| 西峡县| 珲春市| 高邑县| 永和县| 洛川县| 胶南市| 临泉县| 科技| 茶陵县| 绩溪县| 阿拉善盟| 巴彦淖尔市| 常州市| 扶沟县| 谢通门县| 五河县| 夏津县| 方城县| 射洪县| 平塘县| 资阳市|