Java PushbackInputStream skip()方法与示例
PushbackInputStream類skip()方法 (PushbackInputStream Class skip() method)
skip() method is available in java.io package.
skip()方法在java.io包中可用。
skip() method is used to skip the given number of bytes of content from this PushbackInputStream. When the given parameter is less than 0 then no bytes are skipped.
skip()方法用于從此PushbackInputStream跳過給定數量的內容字節。 當給定參數小于0時,則不跳過任何字節。
skip() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
skip()方法是一種非靜態方法,只能通過類對象訪問,如果嘗試使用類名稱訪問該方法,則會收到錯誤消息。
skip() method may throw an exception at the time of skipping bytes of data.
skip()方法在跳過數據字節時可能會引發異常。
IOException: This exception may throw when getting any input/output error while performing or stream close by its close() method or stream unsupport seek() method.
IOException :在執行過程中或通過其close()方法關閉流或不支持流的seek()方法時,如果遇到任何輸入/輸出錯誤,則可能引發此異常。
Syntax:
句法:
public long skip(long number);Parameter(s):
參數:
long number represents the number of bytes to be skipped.
long number表示要跳過的字節數。
Return value:
返回值:
The return type of the method is long, it returns the exact number of bytes skipped.
該方法的返回類型為long ,它返回跳過的確切字節數。
Example:
例:
// Java program to demonstrate the example // of long skip(long number) method of // PushbackInputStreamimport java.io.*;public class SkipOfPBIS {public static void main(String[] args) throws Exception {byte[] b_arr = {97,98,99,100,101,102,103,104,105};InputStream is_stm = null;PushbackInputStream pb_stm = null;try {// Instantiates ByteArrayOutputStream and PushbackInputStreamis_stm = new ByteArrayInputStream(b_arr);pb_stm = new PushbackInputStream(is_stm);// Loop to read till reach its endfor (int i = 0; i < 4; ++i) {// By using read() method is to // convert byte into charchar ch = (char) pb_stm.read();System.out.println("ch: " + ch);// By using skip() method is to// skip the given byte of data // from the streamlong skip = pb_stm.skip(1);System.out.println("pb_stm.skip(1): " + skip);}} catch (Exception ex) {System.out.println(ex.toString());} finally {if (is_stm != null)is_stm.close();if (pb_stm != null)pb_stm.close();}} }Output
輸出量
ch: a pb_stm.skip(1): 1 ch: c pb_stm.skip(1): 1 ch: e pb_stm.skip(1): 1 ch: g pb_stm.skip(1): 1翻譯自: https://www.includehelp.com/java/pushbackinputstream-skip-method-with-example.aspx
總結
以上是生活随笔為你收集整理的Java PushbackInputStream skip()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 动态规划编程面试_面试的前25大动态编程
- 下一篇: ruby三元操作符_在Ruby中使用操作