posts - 73,  comments - 55,  trackbacks - 0

          第一篇、http://www.blueidea.com/bbs/newsdetail.asp?id=996916(里面有很多例子)

          第二篇、徹底明白Java的IO系統(tǒng)(文摘)---JAVA之精髓IO流
          一. 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)存中的一個(gè)緩沖區(qū)作為InputStream使用
          2) StringBufferInputStream:把一個(gè)String對象作為InputStream
          3) FileInputStream:把一個(gè)文件作為InputStream,實(shí)現(xiàn)對文件的讀取操作
          4) PipedInputStream:實(shí)現(xiàn)了pipe的概念,主要在線程中使用
          5) SequenceInputStream:把多個(gè)InputStream合并為一個(gè)InputStream
          2) Out stream
          1) ByteArrayOutputStream:把信息存入內(nèi)存中的一個(gè)緩沖區(qū)中
          2) FileOutputStream:把信息存入文件中
          3) PipedOutputStream:實(shí)現(xiàn)了pipe的概念,主要在線程中使用
          4) SequenceOutputStream:把多個(gè)OutStream合并為一個(gè)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)
          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。兩個(gè)對應(yīng)類實(shí)現(xiàn)的功能相同,字是在操作時(shí)的導(dǎo)向不同。如CharArrayReader:和ByteArrayInputStream的作用都是把內(nèi)存中的一個(gè)緩沖區(qū)作為InputStream使用,所不同的是前者每次從內(nèi)存中讀取一個(gè)字節(jié)的信息,而后者每次從內(nèi)存中讀取一個(gè)字符。
          1.3 兩種不現(xiàn)導(dǎo)向的stream之間的轉(zhuǎn)換
          InputStreamReader和OutputStreamReader:把一個(gè)以字節(jié)為導(dǎo)向的stream轉(zhuǎn)換成一個(gè)以字符為導(dǎo)向的stream。
          2. stream添加屬性
          2.1 “為stream添加屬性”的作用
          運(yùn)用上面介紹的Java中操作IO的API,我們就可完成我們想完成的任何操作了。但通過FilterInputStream和FilterOutStream的子類,我們可以為stream添加屬性。下面以一個(gè)例子來說明這種功能的作用。
          如果我們要往一個(gè)文件中寫入數(shù)據(jù),我們可以這樣操作:
          FileOutStream fs = new FileOutStream(“test.txt”);
          然后就可以通過產(chǎn)生的fs對象調(diào)用write()函數(shù)來往test.txt文件中寫入數(shù)據(jù)了。但是,如果我們想實(shí)現(xiàn)“先把要寫入文件的數(shù)據(jù)先緩存到內(nèi)存中,再把緩存中的數(shù)據(jù)寫入文件中”的功能時(shí),上面的API就沒有一個(gè)能滿足我們的需求了。但是通過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:會(huì)記錄input stream內(nèi)的行數(shù),然后可以調(diào)用getLineNumber()和setLineNumber(int)
          4) PushbackInputStream:很少用到,一般用于編譯器開發(fā)
          2.2.2 用于封裝以字符為導(dǎo)向的InputStream
          1) 沒有與DataInputStream對應(yīng)的類。除非在要使用readLine()時(shí)改用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)生一個(gè)對象時(shí),可指明要打開的文件的性質(zhì):r,只讀;w,只寫;rw可讀寫
          3) 可以直接跳到文件中指定的位置
          4. I/O應(yīng)用的一個(gè)例子
          import java.io.*;
          public class TestIO{
          public static void main(String[] args)
          throws IOException{
          //1.以行為單位從一個(gè)文件讀取數(shù)據(jù)
          BufferedReader in =
          new BufferedReader(
          new FileReader("F:\\nepalon\\TestIO.java"));
          String s, s2 = new String();
          while((s = in.readLine()) != null)
          s2 += s + "\n";
          in.close();

          //1b. 接收鍵盤的輸入
          BufferedReader stdin =
          new BufferedReader(
          new InputStreamReader(System.in));
          System.out.println("Enter a line:");
          System.out.println(stdin.readLine());

          //2. 從一個(gè)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ù)的存儲(chǔ)和恢復(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();

          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)讀取文件時(shí),先把文件內(nèi)容讀到緩存中,當(dāng)調(diào)用in.readLine()時(shí),再從緩存中以字符的方式讀取數(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ū)中,要以字符的形式從一個(gè)String對象中讀取數(shù)據(jù),所以要產(chǎn)生一個(gè)StringReader類型的stream。
          4區(qū)中,對String對象s2讀取數(shù)據(jù)時(shí),先把對象中的數(shù)據(jù)存入緩存中,再從緩沖中進(jìn)行讀取;對TestIO.out文件進(jìn)行操作時(shí),先把格式化后的信息輸出到緩存中,再把緩存中的信息輸出到文件中。
          5區(qū)中,對Data.txt文件進(jìn)行輸出時(shí),是先把基本類型的數(shù)據(jù)輸出屋緩存中,再把緩存中的數(shù)據(jù)輸出到文件中;對文件進(jìn)行讀取操作時(shí),先把文件中的數(shù)據(jù)讀取到緩存中,再從緩存中以基本類型的形式進(jìn)行讀取。注意in5.readDouble()這一行。因?yàn)閷懭氲谝粋€(gè)writeDouble(),所以為了正確顯示。也要以基本類型的形式進(jìn)行讀取。
          6區(qū)是通過RandomAccessFile類對文件進(jìn)行操作。

          第三篇、花1K內(nèi)存實(shí)現(xiàn)高效I/O的RandomAccessFile類(http://www-128.ibm.com/developerworks/cn/java/l-javaio/index.html),解決RandomAccessFile類效率低下的問題,特別是“與JDK1.4新類MappedByteBuffer+RandomAccessFile的對比”部分講了怎樣用jdk自己的功能實(shí)現(xiàn)。

          posted on 2006-06-01 15:04 保爾任 閱讀(249) 評論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 东乌珠穆沁旗| 宜宾市| 海阳市| 永平县| 台前县| 即墨市| 东乌| 大田县| 徐闻县| 双柏县| 札达县| 沁源县| 讷河市| 安福县| 滕州市| 九江县| 昂仁县| 左贡县| 河曲县| 拜城县| 九龙城区| 腾冲县| 张家界市| 广丰县| 柳林县| 西吉县| 布尔津县| 临夏市| 湘潭市| 积石山| 马山县| 新河县| 肥城市| 无极县| 洪泽县| 林州市| 弥勒县| 乌恰县| 论坛| 平顶山市| 澄城县|