ObjectInputStream 的 readStreamHeader() 方法用於允許子類讀取和驗證它們各自的流頭。它讀取並驗證幻數和版本號。
用法
protected void readStreamHeader()throws IOException, StreamCorruptedException
參數
NA
返回
NA
拋出
IOException - 如果從底層 InputStream 讀取時出現 I/O 錯誤
StreamCorruptedException - 如果流中的控製信息不一致。
例子1
import java.io.*;
public class ObjectInputStreamreadStreamHeaderExample1 extends ObjectInputStream {
public ObjectInputStreamreadStreamHeaderExample1(InputStream in) throws IOException {
super(in);
}
public static void main(String[] args) {
String s = "welcome to javaTpoint";
try {
// create a new file with an ObjectOutputStream
FileOutputStream outstream = new FileOutputStream("file1.txt");
ObjectOutputStream objoutstream = new ObjectOutputStream(outstream);
// write something in the file
objoutstream.writeUTF(s);
objoutstream.flush();
// create an ObjectInputStream for the file we created before
ObjectInputStreamreadStreamHeaderExample1 objinstream = new ObjectInputStreamreadStreamHeaderExample1(new FileInputStream("file1.txt"));
// get the class for string and print the name
System.out.println("" + objinstream.resolveObject(objinstream.readUTF()));
objinstream.readStreamHeader();
} catch (Exception ex) {
}
}
}
輸出:
welcome to javaTpoint
例子2
package ObjectInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
public class ObjectInputStreamreadStreamHeaderExample2 extends ObjectInputStream {
public ObjectInputStreamreadStreamHeaderExample2(InputStream in) throws IOException {
super(in);
this.readStreamHeader();
}
public static void main(String[] args) throws IOException {
double[] carray = {21.0, 22.0 , 23.0 , 45.0 , 67.9 , 66.0};
try {
// create file output stream
FileOutputStream fileoutstream = new FileOutputStream("file1.txt");
// create data output stream
DataOutputStream dataoutstream = new DataOutputStream(fileoutstream);
// for each byte in the buffer
for (double b:carray) {
// write character to the dos
dataoutstream.writeDouble(b);
}
// force bytes to the underlying stream
dataoutstream.flush();
// create file input stream
InputStream inpstream = new FileInputStream("file1.txt");
//ObjectInputStreamresolveObjectExample2 objinstream = new ObjectInputStreamresolveObjectExample2(new FileInputStream("file1.txt"));
// create new data input stream
DataInputStream datainpstream = new DataInputStream(inpstream);
// read till end of the stream
for(int i=0;i<=carray.length;i++){
// read double values in float format
float c = datainpstream.readFloat();
// System.out.println("" + objinstream.resolveObject(objinstream.readUTF()));
// print
System.out.print(" "+c);
}
} catch(Exception e) {
}
}
}
輸出:
2.828125 0.0 2.84375 0.0 2.859375 0.0 3.1015625
相關用法
- Java ObjectInputStream readShort()用法及代碼示例
- Java ObjectInputStream readDouble()用法及代碼示例
- Java ObjectInputStream readUnsignedByte()用法及代碼示例
- Java ObjectInputStream readFields()用法及代碼示例
- Java ObjectInputStream readFully()用法及代碼示例
- Java ObjectInputStream readInt()用法及代碼示例
- Java ObjectInputStream read()用法及代碼示例
- Java ObjectInputStream readUTF()用法及代碼示例
- Java ObjectInputStream readField()用法及代碼示例
- Java ObjectInputStream readUnsingedByte()用法及代碼示例
- Java ObjectInputStream readByte()用法及代碼示例
- Java ObjectInputStream readLong()用法及代碼示例
- Java ObjectInputStream readUnsignedShort()用法及代碼示例
- Java ObjectInputStream readObjectOverride()用法及代碼示例
- Java ObjectInputStream readUnshared()用法及代碼示例
- Java ObjectInputStream readChar()用法及代碼示例
- Java ObjectInputStream readObject()用法及代碼示例
- Java ObjectInputStream readLine()用法及代碼示例
- Java ObjectInputStream readBoolean()用法及代碼示例
- Java ObjectInputStream readFloat()用法及代碼示例
注:本文由純淨天空篩選整理自 Java ObjectInputStream readStreamHeader() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。