2010-09-12 59 views
2

我現在不知道,就是這兩種方法之間的區別:SkipBytes與DataInputStream的跳轉之間的區別?

DataInputStream.skipBytesDataInputStream.skip

我知道的一個事實,即skip必須來自InputStreamskipBytesDataInput,但仍是有什麼區別。你知道,當在J2ME中使用流時,事情變得非常棘手,所以我需要需要來知道!

從JSR-75中的FileConnection返回的輸入/數據輸入流與任何其他此類流處理方式有什麼不同?

謝謝!

回答

2

DataInputStream

public final int skipBytes(int n) throws IOException { 
    int total = 0; 
    int cur = 0; 

    while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) { 
     total += cur; 
    } 

    return total; 
    } 

,你可以從代碼中看到,skipBytes使用skip (InputStream.skip)

,我唯一能說的是,如果在你的包裹的inputStream(InputStream的內部DataInputStream類)的變化數據由另一個線程,那麼skipBytes和skip的結果可能會不同。但是如果你的應用程序只使用單線程,那麼skipBytes和skip就是一樣的。

+0

非常感謝!然後它解釋它。 – 2010-09-12 13:22:34

+0

'in.skip(x)'(也可以通過'skip()'調用)不保證跳過x個字節,這就是爲什麼skipBytes的DataInputStream使用這個循環。但是,由於'InputStream.skip()'允許根據[合同]返回零(http://docs.oracle.com/javase/1.4.2/docs/api/java/io/InputStream.html#跳過%28long%29),即使'skipBytes'也不保證跳過(如果一個跳過返回0,循環將退出)。 – 2013-03-30 22:07:15

4

此外,skip()需要很長的參數,所以您可以一次跳過更多的字節。用於大文件

相關問題