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


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


ObjectInputStream 類的 readUnsignedShort() 方法讀取一個無符號的 16 位短字節。

用法

public int readUnsignedShort() throws IOException

參數

沒有傳遞參數。

返回

16 位短讀。

拋出

IOException

例子1

import java.io.*;

public class ObjectInputStreamreadunsignedshortExample1 {
   public static void main(String[] args) {
    short b = 32767;
      try {
         FileOutputStream outstream = new FileOutputStream("file1.txt");
         ObjectOutputStream objoutstream = new ObjectOutputStream(outstream);
         objoutstream.writeShort(b);
      
         objoutstream.flush();

         ObjectInputStream objinstream = new ObjectInputStream(new FileInputStream("file1.txt"));
         // read and print the short value
         System.out.println(" short value:" + objinstream.readUnsignedShort());

         
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出:

short value:32767

例子2

import java.io.*; 
  
public class ObjectInputStreamreadunsignedshortExample2  
{ 
    public static void main(String[] args) throws IOException 
    { 
        FileOutputStream outstream = new FileOutputStream("file1.txt"); 
        ObjectOutputStream objoutStream = new ObjectOutputStream(outstream);    
        ObjectInputStream objinStream   =  new ObjectInputStream(new FileInputStream("file1.txt"));
        objoutStream.writeShort(121212); 
        objoutStream.flush();
        // Use of readUnsignedShort() to read Short.
        System.out.println("readUnsignedShort():" + objinStream.readUnsignedShort()); 
    } 
}

輸出:

readUnsignedShort():55676




相關用法


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