RandomAccessFile類readShort()方法
- readShort() 方法可在
java.io
包。 - readShort() 方法用於從此 RandomAccessFile 讀取帶符號的 16 位短整數值。
- readShort() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
-
readShort() 方法可能在讀取短整數值時拋出異常。
- IOException:此異常在執行輸入/輸出操作時可能會拋出異常。
- EOF異常: 當文件指針在讀取 2 個字節之前到達 EOF(文件結束)時,可能會拋出此異常。
用法:
public final short readShort();
參數:
- 它不接受任何參數。
返回值:
這個方法的返回類型是short
,它返回來自這個 RandomAccessFile 的 2 個字節的數據,作為一個短整數值進行操作。
例:
// Java program to demonstrate the example
// of short readShort() method of
// RandomAccessFile
import java.io.*;
class RAFReadShort
public static void main(String[] args) throws Exception {
// Instantiate a random access file
// object with file name and permissions
RandomAccessFile ra_f = new RandomAccessFile("e:/includehelp.txt", "rw");
// By using writeShort() method is to
// write short to the file
ra_f.writeShort(10204);
// Initially set the file pointer
// is at 1 for reading the file
ra_f.seek(1);
// By using readShort() method is to
// read a short value up to signed 16 bit
// from this file
short s = ra_f.readShort();
System.out.println("ra_f.readShort():" + s);
// By using close() method isto
// close this stream ran_f
ra_f.close();
}
}
輸出
ra_f.readShort():-9216
相關用法
- Java RandomAccessFile readByte()用法及代碼示例
- Java RandomAccessFile readChar()用法及代碼示例
- Java RandomAccessFile readUnsignedByte()用法及代碼示例
- Java RandomAccessFile readInt()用法及代碼示例
- Java RandomAccessFile readDouble()用法及代碼示例
- Java RandomAccessFile readLong()用法及代碼示例
- Java RandomAccessFile readUTF()用法及代碼示例
- Java RandomAccessFile readLine()用法及代碼示例
- Java RandomAccessFile readUnsignedShort()用法及代碼示例
- Java RandomAccessFile readFloat()用法及代碼示例
- Java RandomAccessFile close()用法及代碼示例
- Java RandomAccessFile writeUTF()用法及代碼示例
- Java RandomAccessFile writeFloat()用法及代碼示例
- Java RandomAccessFile getFD()用法及代碼示例
- Java RandomAccessFile writeChar()用法及代碼示例
- Java RandomAccessFile writeBytes()用法及代碼示例
- Java RandomAccessFile seek()用法及代碼示例
- Java RandomAccessFile skipBytes()用法及代碼示例
- Java RandomAccessFile writeDouble()用法及代碼示例
- Java RandomAccessFile getChannel()用法及代碼示例
注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java RandomAccessFile readShort() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。