乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      FileInputStream/FileOutputStream的應(yīng)用

       My鏡像站 2012-01-17

      這是一對(duì)繼承于InputStream和OutputStream的類,用于本地文件讀寫(xiě)(二進(jìn)制格式讀寫(xiě)并且是順序讀寫(xiě),讀和寫(xiě)要分別創(chuàng)建出不同的文件流對(duì)象);

      本地文件讀寫(xiě)編程的基本過(guò)程為:

      ①  生成文件流對(duì)象(對(duì)文件讀操作時(shí)應(yīng)該為FileInputStream類,而文件寫(xiě)應(yīng)該為FileOutputStream類);

      ②  調(diào)用FileInputStream或FileOutputStream類中的功能函數(shù)如read()、write(int b)等)讀寫(xiě)文件內(nèi)容;

      ③  關(guān)閉文件(close())。

      實(shí)例:流文件讀寫(xiě)

      流文件的單元是字節(jié),所以它不但可以讀寫(xiě)文本文件,也可以讀寫(xiě)圖片、聲音、影像文件,這種特點(diǎn)非常有用,因?yàn)槲覀兛梢园堰@種文件變成流,然后在網(wǎng)絡(luò)上傳輸。

      問(wèn)題是有了通用的流文件以后,為什么還要專門(mén)的字符流呢?這是因?yàn)槲谋究梢杂貌煌姆绞酱鎯?chǔ),可以是普通的文本(UTF-8編碼方式),ASCII文本和Unicode文本,字符流對(duì)象可以進(jìn)行必要的轉(zhuǎn)換,從而讀出正確的文本。

      有人認(rèn)為流文件不能讀寫(xiě)文本文件,這其實(shí)是個(gè)誤會(huì),因?yàn)槲谋疚募举|(zhì)上也是由字節(jié)組成的,當(dāng)然是流文件的一種。作為讀寫(xiě)文件的全體,這是沒(méi)問(wèn)題的,但是,如果要處理每次讀入的內(nèi)容,就最好使用字符流。

      所以在文本文件處理時(shí),使用字符流是個(gè)最常用的方法。

      樣例:

      import java.io.*;

      public class FileStreamDemo {

      public static void main(String[] args) throws IOException {

      //創(chuàng)建兩個(gè)文件,face.gif是已經(jīng)存在文件,newFace.gif是新創(chuàng)建的文件

      File inFile = new File("face.gif");

      File outFile = new File("newFace.gif");

      //創(chuàng)建流文件讀入與寫(xiě)出類

      FileInputStream inStream = new FileInputStream(inFile);

      FileOutputStream outStream = new FileOutputStream(outFile);

      //通過(guò)available方法取得流的最大字符數(shù)

      byte[] inOutb = new byte[inStream.available()];

      inStream.read(inOutb);  //讀入流,保存在byte數(shù)組

      outStream.write(inOutb);  //寫(xiě)出流,保存在文件newFace.gif中

      inStream.close();

      outStream.close();

      }

      }

      實(shí)例:讀寫(xiě)任意大文件應(yīng)用

      因?yàn)閎yte數(shù)組最大存儲(chǔ)值不超過(guò)64M,所以當(dāng)一個(gè)文件大于60M 的時(shí)候,需要分開(kāi)幾個(gè)流操作。我們把上面的程序作一個(gè)修改,就可以寫(xiě)入任意大小的文件。這個(gè)程序應(yīng)用了FileInputStream類的方法如下:

      read(byte[] b,int off,int len)

      把特定位置的流內(nèi)容讀入數(shù)組,已經(jīng)讀入byte[]數(shù)組的內(nèi)容,會(huì)在流文件中刪除。

      程序運(yùn)行的結(jié)果會(huì)產(chǎn)生一個(gè)新文件。

      樣例:

      import java.io.*;

      public class FileStreamDemo2 {

      public static void main(String[] args) throws IOException {

      //創(chuàng)建兩個(gè)文件

      File inFile = new File("tcty36.rm");

      File outFile = new File("newtcty36.rm");

      //最大的流為60Mb,當(dāng)文件的容量大于60Mb的時(shí)候便分開(kāi)流

      final int MAX_BYTE = 60000000;

      long streamTotal = 0;  //接受流的容量

      int streamNum = 0;  //流需要分開(kāi)的數(shù)量

      int leave = 0;  //文件剩下的字符數(shù)

      byte[] inOutb;  //byte數(shù)組接受文件的數(shù)據(jù)

      //創(chuàng)建流文件讀入與寫(xiě)出類

      FileInputStream inStream = new FileInputStream(inFile);

      FileOutputStream outStream = new FileOutputStream(outFile);

      //通過(guò)available方法取得流的最大字符數(shù)

      streamTotal = inStream.available();

      //取得流文件需要分開(kāi)的數(shù)量

      streamNum = (int)Math.floor(streamTotal/MAX_BYTE);

      //分開(kāi)文件之后,剩余的數(shù)量

      leave = (int)streamTotal % MAX_BYTE;

      //文件的容量大于60Mb時(shí)進(jìn)入循環(huán)

      if (streamNum > 0) {

      for(int i = 0; i < streamNum; ++i){

      inOutb = new byte[MAX_BYTE];

      //讀入流,保存在byte數(shù)組

      inStream.read(inOutb, 0, MAX_BYTE);

      outStream.write(inOutb);  //寫(xiě)出流

      outStream.flush();  //更新寫(xiě)出的結(jié)果

      }

      }

      //寫(xiě)出剩下的流數(shù)據(jù)

      inOutb = new byte[leave];

      inStream.read(inOutb, 0, leave);

      outStream.write(inOutb);

      outStream.flush();

      inStream.close();

      outStream.close();

      }

      }

      六、管道PipedInputStream/PipedOutputStream類:

      當(dāng)需要在兩個(gè)線程中讀寫(xiě)數(shù)據(jù)的時(shí)候,由于線程的并發(fā)執(zhí)行,讀寫(xiě)的同步問(wèn)題可能會(huì)發(fā)生困難,這時(shí)候可以使用管道,管道事實(shí)上是一個(gè)隊(duì)列。

      管 道是由系統(tǒng)維護(hù)的一個(gè)緩沖區(qū),當(dāng)然程序員也可以自己直接指定該緩沖區(qū)的大?。ㄖ恍枰O(shè)置管道流類中的PIPE_SIZE屬性的值)。當(dāng)生產(chǎn)者生產(chǎn)出數(shù)據(jù) 后,只需要將數(shù)據(jù)寫(xiě)入管道中,消費(fèi)者只需要從管道中讀取所需要的數(shù)據(jù)。利用管道的這種機(jī)制,可以將一個(gè)線程的輸出結(jié)果直接連接到另一個(gè)線程的輸入端口,實(shí) 現(xiàn)兩者之間的數(shù)據(jù)直接傳送。

      線程1
      線程2
      臨時(shí)文件
      管道

      1.管道的連接:

      方法之一是通過(guò)構(gòu)造函數(shù)直接將某一個(gè)程序的輸出作為另一個(gè)程序的輸入,在定義對(duì)象時(shí)指明目標(biāo)管道對(duì)象

      PipedInputStream pInput=new PipedInputStream();

      PipedOutputStream pOutput= new PipedOutputStream(pInput);

      方法之二是利用雙方類中的任一個(gè)成員函數(shù) connect()相連接

      PipedInputStream pInput=new PipedInputStream();

      PipedOutputStream pOutput= new PipedOutputStream();

      pinput.connect(pOutput);

      2.管道的輸入與輸出:

      輸出管道對(duì)象調(diào)用write()成員函數(shù)輸出數(shù)據(jù)(即向管道的輸入端發(fā)送數(shù)據(jù));而輸入管道對(duì)象調(diào)用read()成員函數(shù)可以讀起數(shù)據(jù)(即從輸出管道中獲得數(shù)據(jù))。這主要是借助系統(tǒng)所提供的緩沖機(jī)制來(lái)實(shí)現(xiàn)的。

      實(shí)例:Java的管道的輸入與輸出

      import java.io.*;

      public class PipedIO //程序運(yùn)行后將sendFile文件的內(nèi)容拷貝到receiverFile文件中

      {

      public static void main(String args[])

      {

      try

      {

      //構(gòu)造讀寫(xiě)的管道流對(duì)象

      PipedInputStream pis=new PipedInputStream();

      PipedOutputStream pos=new PipedOutputStream();

      //實(shí)現(xiàn)關(guān)聯(lián)

      pos.connect(pis);

      //構(gòu)造兩個(gè)線程,并且啟動(dòng)。

      new Sender(pos,"c:\\text2.txt").start();

      new Receiver(pis,"c:\\text3.txt").start();

      }

      catch(IOException e)

      {

      System.out.println("Pipe Error"+ e);

      }

      }

      }

      //線程發(fā)送

      class Sender extends Thread

      {

      PipedOutputStream pos;

      File file;

      //構(gòu)造方法

      Sender(PipedOutputStream pos, String fileName)

      {

      this.pos=pos;

      file=new File(fileName);

      }

      //線程運(yùn)行方法

      public void run()

      {

      try

      {

      //讀文件內(nèi)容

      FileInputStream fs=new FileInputStream(file);

      int data;

      while((data=fs.read())!=-1)

      {

      //寫(xiě)入管道始端

      pos.write(data);

      }

      pos.close();

      }

      catch(IOException e)

      {

      System.out.println("Sender Error" +e);

      }

      }

      }

      //線程讀

      class Receiver extends Thread

      {

      PipedInputStream pis;

      File file;

      //構(gòu)造方法

      Receiver(PipedInputStream pis, String fileName)

      {

      this.pis=pis;

      file=new File(fileName);

      }

      //線程運(yùn)行

      public void run()

      {

      try

      {

      //寫(xiě)文件流對(duì)象

      FileOutputStream fs=new FileOutputStream(file);

      int data;

      //從管道末端讀

      while((data=pis.read())!=-1)

      {

      //寫(xiě)入本地文件

      fs.write(data);

      }

      pis.close();

      }

      catch(IOException e)

      {

      System.out.println("Receiver Error" +e);

      }

      }

      }

      七、隨機(jī)文件讀寫(xiě):RandomAccessFile類

      它直接繼承于Object類而非InputStream/OutputStream類,從而可以實(shí)現(xiàn)讀寫(xiě)文件中任何位置中的數(shù)據(jù)(只需要改變文件的讀寫(xiě)位置的指針)。

      編程步驟:

      ① 生成流對(duì)象并且指明讀寫(xiě)類型;

      ② 移動(dòng)讀寫(xiě)位置;

      ③ 讀寫(xiě)文件內(nèi)容;

      ④ 關(guān)閉文件。

      另外由于RandomAccessFile類實(shí)現(xiàn)了DataOutput與DataInput接口,因而利用它可以讀寫(xiě)Java中的不同類型的基本類型數(shù)據(jù)(比如采用readLong()方法讀取長(zhǎng)整數(shù),而利用readInt()方法可以讀出整數(shù)值等)。

      程序?qū)嵗?/strong>

      利用隨機(jī)數(shù)據(jù)流RandomAccessFile類來(lái)實(shí)現(xiàn)記錄用戶在鍵盤(pán)的輸入,每執(zhí)行一次,將用戶的鍵盤(pán)輸入存儲(chǔ)在指定的UserInput.txt文件中。

      import java.io.*;

      public class RandomFileRW

      {

      public static void main(String args[])

      {

      StringBuffer buf=new StringBuffer();

      char ch;

      try

      {

      while( (ch=(char)System.in.read()) !='\n')

      {

      buf.append(ch);

      }

      //讀寫(xiě)方式可以為"r" or "rw"

      RandomAccessFile myFileStream=new RandomAccessFile("c:\\UserInput.txt","rw");

      myFileStream.seek(myFileStream.length()) ;

      myFileStream.writeBytes(buf.toString());

      //將用戶從鍵盤(pán)輸入的內(nèi)容添加到文件的尾部

      myFileStream.close();

      }

      catch(IOException e)

      {

      }

      }

      }

      八、DataInput/DataOutput接口:

      實(shí) 現(xiàn)與機(jī)器無(wú)關(guān)的各種數(shù)據(jù)格式讀寫(xiě)(如readChar() 、readInt()、readLong()、readFloat(),而readLine()將返回一個(gè)String)。其中 RandomAccessFile類實(shí)現(xiàn)了該接口,具有比FileInputStream或FileOutputStream類更靈活的數(shù)據(jù)讀寫(xiě)方式。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多