當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Objectinputstream skipBytes()用法及代碼示例


ObjectInputStream 類的 skipBytes() 方法用於通過作為參數傳遞的數字跳過字節。

用法

public int skipBytes(int len) throws IOException

參數

len 作為要跳過的字節數

返回

實際跳過的字節數。

拋出

IOException

例子1

import java.io.*;

public class ObjectInputStreamreadunskipByteExample1 {
   public static void main(String[] args) {
      String s = "javaTpoint";
      
      try {
         FileOutputStream outstream = new FileOutputStream("file1.txt");
         ObjectOutputStream objoutstream = new ObjectOutputStream(outstream);
         objoutstream.writeUTF(s);
         objoutstream.writeUTF("javaTpoint provide tutorial for all languages");
         objoutstream.flush();
         ObjectInputStream objinstream = new ObjectInputStream(new FileInputStream("file1.txt"));
         objinstream.skipBytes(6);
         for (int i = 0; i < objinstream.available() - 6; i++) {
            System.out.print("" + (char) objinstream.readByte());
         }
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出:

Tpoint
javaTpoint  provide

例子2

import java.io.*; 
  
public class ObjectInputStreamreadunskipByteExample2 
{ 
    public static void main(String[] args) throws IOException, ClassNotFoundException 
    { 
        FileOutputStream outstream = new FileOutputStream("file1.txt"); 
        ObjectOutputStream objoutStream = new ObjectOutputStream(outstream); 
        ObjectInputStream objinStream = new ObjectInputStream(new FileInputStream("file1.txt")); 
        objoutStream.writeUTF(" Welcome to javatpoint"); 
        objoutStream.flush(); 
        objinStream.skipBytes(7); 
          
        for (int i = 2; i < objinStream.available(); i++)  
        { 
            System.out.print((char) objinStream.readByte()); 
        } 
    } 
}

輸出:

ome to j




相關用法


注:本文由純淨天空篩選整理自 Java Objectinputstream skipBytes() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。