ObjectOutputStream類writeObject()方法
- writeObject() 方法可在
java.io
包。 - writeObject() 方法用於寫入給定的 2 字節短值。
- writeObject() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
- writeObject() 方法可能會在寫短時拋出異常。
IOException:在寫入輸出流時出現任何輸入/輸出錯誤時,可能會拋出此異常。
用法:
public void writeShort(int value);
參數:
int value
– 表示一個整數,表示要寫入的短值。
返回值:
該方法的返回類型是void
,它什麽都不返回。
例:
// Java program to demonstrate the example
// of void writeShort(int value) method of
// ObjectOutputStream
import java.io.*;
public class WriteShortOfOOS {
public static void main(String[] args) throws Exception {
// Instantiates ObjectOutputStream , ObjectInputStream
// FileInputStream and FileOutputStream
FileOutputStream file_out_stm = new FileOutputStream("D:\\includehelp.txt");
ObjectOutputStream obj_out_stm = new ObjectOutputStream(file_out_stm);
FileInputStream file_in_stm = new FileInputStream("D:\\includehelp.txt");
ObjectInputStream obj_in_stm = new ObjectInputStream(file_in_stm);
// By using writeShort() method is to
// write the short to the stream
obj_out_stm.writeShort(100);
obj_out_stm.writeShort(124);
obj_out_stm.writeShort(168);
obj_out_stm.writeShort(152);
obj_out_stm.flush();
// By using readShort() method is to
// read the int and convert it into short
for (int i = 0; i < 4; ++i) {
short sh = (short) obj_in_stm.readShort();
System.out.println("obj_in_stm.readShort():" + sh);
}
// By using close() method is to
// close all the streams
System.out.println("Stream Shutdown... ");
file_in_stm.close();
file_out_stm.close();
obj_in_stm.close();
obj_out_stm.close();
}
}
輸出
obj_in_stm.readShort():100 obj_in_stm.readShort():124 obj_in_stm.readShort():168 obj_in_stm.readShort():152 Stream Shutdown...
相關用法
- Java ObjectOutputStream writeFields()用法及代碼示例
- Java ObjectOutputStream writeBytes()用法及代碼示例
- Java ObjectOutputStream writeBoolean()用法及代碼示例
- Java ObjectOutputStream writeByte()用法及代碼示例
- Java ObjectOutputStream writeObject()用法及代碼示例
- Java ObjectOutputStream writeFloat()用法及代碼示例
- Java ObjectOutputStream writeUnshared()用法及代碼示例
- Java ObjectOutputStream writeChar()用法及代碼示例
- Java ObjectOutputStream writeChars()用法及代碼示例
- Java ObjectOutputStream writeInt()用法及代碼示例
- Java ObjectOutputStream writeLong()用法及代碼示例
- Java ObjectOutputStream writeDouble()用法及代碼示例
- Java ObjectOutputStream useProtocolVersion()用法及代碼示例
- Java ObjectOutputStream flush()用法及代碼示例
- Java ObjectOutputStream drain()用法及代碼示例
- Java ObjectOutputStream enableReplaceObject()用法及代碼示例
- Java ObjectOutputStream reset()用法及代碼示例
- Java ObjectOutputStream close()用法及代碼示例
- Java ObjectOutputStream replaceObject()用法及代碼示例
- Java ObjectInputStream readDouble()用法及代碼示例
注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java ObjectOutputStream writeShort() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。