ObjectInputStream 類的 readInt() 方法用於讀取 32 位 int。
用法
Public int readInt() throws IOException
參數
沒有傳遞參數。
返回
讀取的 32 位整數。
拋出
EOFException - 如果到達文件末尾。
IOException - 如果發生其他 I/O 錯誤。
例子1
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class ObjectInputStreamreadIntExample1 {
public static void main(String[] args) throws IOException {
int[] buf = {1 , 2 , 6 , 6 , 7 , 3};
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 (int b:buf) {
// write character to the dos
dataoutstream.writeInt(b);
}
// force bytes to the underlying stream
dataoutstream.flush();
// create file input stream
InputStream inpstream = new FileInputStream("file1.txt");
// create new data input stream
DataInputStream datainpstream = new DataInputStream(inpstream);
// read till end of the stream
while(datainpstream.available()>0) {
// read character
int c = datainpstream.readInt();
// print
System.out.print(" "+c);
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
輸出:
1 2 6 6 7 3
例子2
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class ObjectInputStreamreadIntExample2 {
public static void main(String[] args) throws IOException {
int[] carray = {21 , 22 , 23 , 45 , 67 , 66};
int barray[];
try {
// create file output stream
FileOutputStream fileoutstream = new FileOutputStream("barray");
// create data output stream
DataOutputStream dataoutstream = new DataOutputStream(fileoutstream);
// for each byte in the buffer
for (int b:carray) {
// write character to the dos
dataoutstream.writeInt(b);
}
// force bytes to the underlying stream
dataoutstream.flush();
// create file input stream
InputStream inpstream = new FileInputStream("barray");
// 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
int c = datainpstream.readInt();
// print
System.out.print(" "+c);
}
} catch(Exception e) {
}
}
}
輸出:
21 22 23 45 67 66
相關用法
- Java ObjectInputStream readInt()用法及代碼示例
- Java ObjectInputStream readDouble()用法及代碼示例
- Java ObjectInputStream readUnsignedByte()用法及代碼示例
- Java ObjectInputStream readFields()用法及代碼示例
- Java ObjectInputStream readFully()用法及代碼示例
- Java ObjectInputStream read()用法及代碼示例
- Java ObjectInputStream readUTF()用法及代碼示例
- Java ObjectInputStream readField()用法及代碼示例
- Java ObjectInputStream readShort()用法及代碼示例
- Java ObjectInputStream readUnsingedByte()用法及代碼示例
- Java ObjectInputStream readByte()用法及代碼示例
- Java ObjectInputStream readLong()用法及代碼示例
- Java ObjectInputStream readStreamHeader()用法及代碼示例
- Java ObjectInputStream readUnsignedShort()用法及代碼示例
- Java ObjectInputStream readObjectOverride()用法及代碼示例
- Java ObjectInputStream readUnshared()用法及代碼示例
- Java ObjectInputStream readChar()用法及代碼示例
- Java ObjectInputStream readObject()用法及代碼示例
- Java ObjectInputStream readLine()用法及代碼示例
- Java ObjectInputStream readBoolean()用法及代碼示例
注:本文由純淨天空篩選整理自 Java ObjectInputStream readInt() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。