void writeShort(int val)


描述

所述java.io.ObjectOutputStream.writeShort(int val)方法写入一个16位的短。

声明

以下是java.io.ObjectOutputStream.writeShort()方法的声明。

public void writeShort(int val)

参数

val - 要写入的短值。

返回值

此方法不返回值。

异常

IOException - 如果在写入基础流时发生I / O错误。

实例

以下示例显示了java.io.ObjectOutputStream.writeShort()方法的用法。

package com.tutorialspoint;

import java.io.*;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      short s = 174;
      short s2 = 1654;

      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeShort(s);
         oout.writeShort(s2);

         // close the stream
         oout.close();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read and print what we wrote before
         System.out.println("" + ois.readShort());
         System.out.println("" + ois.readShort());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果

174
1654