漁人碼頭

          天行健,君子以自強不息。地勢坤,君子以厚德載物。
          posts - 12, comments - 16, trackbacks - 0, articles - 43
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Java IO

          Posted on 2007-03-05 22:41 Fisher 閱讀(237) 評論(0)  編輯  收藏 所屬分類: Java基礎(chǔ)


          1、流:
          它是通過緩沖機制將數(shù)據(jù)從生產(chǎn)者(如鍵盤、磁盤文件、內(nèi)存或其他設(shè)備)傳送到接受該數(shù)據(jù)的消費者(如屏幕、文件或者內(nèi)存等)的這一過程的抽象。
          2、有關(guān)的Java包:
          Java.io包中包括許多類提供許多有關(guān)文件的各個方面操作。
          3、有關(guān)文件名及目錄名的類:File 類獨立于系統(tǒng)平臺,利用構(gòu)造函數(shù)
          File( String path)、
          File(String path, String FileName)、
          File(File dir, String name) 等創(chuàng)建出File 對象;再利用canRead() 、canWrite()、 getParent()、 getPath()等成員函數(shù)實現(xiàn)對文件的各個屬性的操作。
          import java.io.*;
          public class FileTest
          { public static void main(String []args)
          {
          String FileName="C:\\temp\\myfile.dat"
          File myFile=new File(FileName);
          If( ! myFile. exists() )
          { System.err.println("Can't Find " + FileName);
          return;
          }
          System.out.println("File " + FileName + "is " +myFile.length() + "bytes Long !");
          If( myFile. isDirectory() )
          { System.err.println("File" + FileName +"Is a Directory !");
          return;
          }
          }
          }
          4、有關(guān)文件內(nèi)容(數(shù)據(jù))操作的類:
          4.1 輸入輸出抽象基類InputStream/OutputStream ,實現(xiàn)文件內(nèi)容操作的基本功能函數(shù)read()、 write()、close()、skip()等;一般都是創(chuàng)建出其派生類對象(完成指定的特殊功能)來實現(xiàn)文件讀寫。在文件讀寫的編程過程中主要應(yīng)該注意異常處理的技術(shù)。
          4.2 FileInputStream/FileOutputStream:
          用于本地文件讀寫(二進制格式讀寫并且是順序讀寫,讀和寫要分別創(chuàng)建出不同的文件流對象);
          本地文件讀寫編程的基本過程為:
          ① 生成文件流對象(對文件讀操作時應(yīng)該為FileInputStream類,而文件寫應(yīng)該為FileOutputStream類);
          ② 調(diào)用FileInputStream或FileOutputStream類中的功能函數(shù)如read()、write(int b)等)讀寫文件內(nèi)容;
          ③ 關(guān)閉文件(close())。
          4.3 PipedInputStream/PipedOutputStream:
          用于管道輸入輸出(將一個程序或一個線程的輸出結(jié)果直接連接到另一個程序或一個線程的輸入端口,實現(xiàn)兩者數(shù)據(jù)直接傳送。操作時需要連結(jié));
          4.3.1 管道的連接:
          方法之一是通過構(gòu)造函數(shù)直接將某一個程序的輸出作為另一個程序的輸入,在定義對象時指明目標管道對象
          PipedInputStream pInput=new PipedInputStream();
          PipedOutputStream pOutput= new PipedOutputStream(pInput);
          方法之二是利用雙方類中的任一個成員函數(shù) connect()相連接
          PipedInputStream pInput=new PipedInputStream();
          PipedOutputStream pOutput= new PipedOutputStream();
          pinput.connect(pOutput);
          4.3.2 管道的輸入與輸出:
          輸出管道對象調(diào)用write()成員函數(shù)輸出數(shù)據(jù)(即向管道的輸入端發(fā)送數(shù)據(jù));而輸入管道對象調(diào)用read()成員函數(shù)可以讀起數(shù)據(jù)(即從輸出管道中獲得數(shù)據(jù))。這主要是借助系統(tǒng)所提供的緩沖機制來實現(xiàn)的。
          4.4、隨機文件讀寫:
          RandomAccessFile類(它直接繼承于Object類而非InputStream/OutputStream類),從而可以實現(xiàn)讀寫文件中任何位置中的數(shù)據(jù)(只需要改變文件的讀寫位置的指針)。
          隨機文件讀寫編程的基本過程為:
          ① 生成流對象并且指明讀寫類型;
          ② 移動讀寫位置;
          ③ 讀寫文件內(nèi)容;
          ④ 關(guān)閉文件。
          StringBuffer buf=new StringBuffer();
          char ch;
          while( (ch=(char)System.in.read()) !='\n')
          {
          buf.append( ch);
          } //讀寫方式可以為"r" or "rw"
          RandomAccessFile myFileStream=new RandomAccessFile("myFile.dat"," rw");
          myFileStream . seek(myFileStream.length()) ;
          myFileStream.writeBytes(buf.toString()); //將用戶從鍵盤輸入的內(nèi)容添加到文件的尾部
          myFileStream.close();
          4.5 DataInput/DataOutput接口:實現(xiàn)與機器無關(guān)的各種數(shù)據(jù)格式讀寫(如readChar() 、readInt()、readLong()、readFloat(),而readLine()將返回一個String)。其中RandomAccessFile類實現(xiàn)了該接口,具有比FileInputStream或FileOutputStream類更靈活的數(shù)據(jù)讀寫方式。
          4.6 標準輸入輸出流:System.in(如:char c=System.in.read())和System.out(如:System.out.println()、System.out.println())。
          try
          { char ch=System.in.read(); //返回二進制數(shù)據(jù)(低8位為鍵盤的ASCII碼)
          }
          catch(IOException e)
          {
          }
          4.7、文件操作的一般方法:
          (1)生成一個輸入輸出文件類的對象(根據(jù)所要操作的類型);
          (2)調(diào)用此類的成員函數(shù)實現(xiàn)文件數(shù)據(jù)內(nèi)容的讀寫;
          (3)關(guān)閉此文件。

          ?


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 耿马| 毕节市| 三都| 梁河县| 水城县| 梁山县| 密云县| 福贡县| 连云港市| 滁州市| 新营市| 辽宁省| 云阳县| 石阡县| 香港 | 莱芜市| 鹿邑县| 吴桥县| 曲周县| 磴口县| 汾阳市| 沈阳市| 鸡泽县| 宁陕县| 盘山县| 康平县| 阜平县| 三原县| 嘉荫县| 达尔| 郑州市| 东港市| 永善县| 石景山区| 开阳县| 甘南县| 察哈| 郎溪县| 岳池县| 汤阴县| 城市|