java 檔案流重定位,seek ,mark reset.

來源:互聯網
上載者:User

Java  InputStream  的mark 和reset操作
mark用於標記當前位置;FileInputStream不支援mark和reeset兩個函數;

但是BufferedInputStream支援此操作;

通過mark(readlimit)標記後,後續調用reset()函數,就會使檔案流重新回到mark位置,達到重新利用。

如果不是用mark,那麼你讀到檔案末尾後就不能繼續重新讀了。

mark 和reset要配合使用。


   FileInputStream infile = new FileInputStream("test.txt");
   buffedfile = new BufferedInputStream(infile);
   buffedfile.mark(available());
//對buffedfile 進行讀取操作,讀取之後

buffedfile.reset();

 

然後你可以繼續使用buffedfile。否則你就不能再用了。此程式相當於從檔案流第一offset開始使用,當然,如果你read n個位元組之後,然後再mark,那麼你下次reset,就從n以後開始利用檔案流了。

 

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

類似C語言的seek 用法。
         randomFile = new RandomAccessFile("fileName", "rw");
       long fileLength = randomFile.length();
       randomFile.seek(100);

//從offset為100地方開始對檔案流進行操作。

。。。。。。。。。。。。。。。。。


    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.