📄 randomaccessfile.java
字号:
public void write(byte b[], int off, int len) throws IOException {
writeBytes(b, off, len);
}
// 'Random access' stuff
/**
* Returns the current offset in this file.
*
* @return the offset from the beginning of the file, in bytes,
* at which the next read or write occurs.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
public native long getFilePointer() throws IOException;
/**
* Sets the file-pointer offset, measured from the beginning of this
* file, at which the next read or write occurs. The offset may be
* set beyond the end of the file. Setting the offset beyond the end
* of the file does not change the file length. The file length will
* change only by writing after the offset has been set beyond the end
* of the file.
*
* @param pos the offset position, measured in bytes from the
* beginning of the file, at which to set the file
* pointer.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
public native void seek(long pos) throws IOException;
/**
* Returns the length of this file.
*
* @return the length of this file.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
public native long length() throws IOException;
/**
* Closes this random access file stream and releases any system
* resources associated with the stream.
*
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
public native void close() throws IOException;
//
// Some "reading/writing Java data types" methods stolen from
// DataInputStream and DataOutputStream.
//
/**
* Reads a <code>boolean</code> from this file. This method reads a
* single byte from the file. A value of <code>0</code> represents
* <code>false</code>. Any other value represents <code>true</code>.
* This method blocks until the byte is read, the end of the stream
* is detected, or an exception is thrown.
*
* @return the <code>boolean</code> value read.
* @exception EOFException if this file has reached the end.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
public final boolean readBoolean() throws IOException {
int ch = this.read();
if (ch < 0)
throw new EOFException();
return (ch != 0);
}
/**
* Reads a signed 8-bit value from this file. This method reads a
* byte from the file. If the byte read is <code>b</code>, where
* <code>0 <= b <= 255</code>,
* then the result is:
* <ul><code>
* (byte)(b)
*</code></ul>
* <p>
* This method blocks until the byte is read, the end of the stream
* is detected, or an exception is thrown.
*
* @return the next byte of this file as a signed 8-bit
* <code>byte</code>.
* @exception EOFException if this file has reached the end.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
public final byte readByte() throws IOException {
int ch = this.read();
if (ch < 0)
throw new EOFException();
return (byte)(ch);
}
/**
* Reads an unsigned 8-bit number from this file. This method reads
* a byte from this file and returns that byte.
* <p>
* This method blocks until the byte is read, the end of the stream
* is detected, or an exception is thrown.
*
* @return the next byte of this file, interpreted as an unsigned
* 8-bit number.
* @exception EOFException if this file has reached the end.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
public final int readUnsignedByte() throws IOException {
int ch = this.read();
if (ch < 0)
throw new EOFException();
return ch;
}
/**
* Reads a signed 16-bit number from this file. The method reads 2
* bytes from this file. If the two bytes read, in order, are
* <code>b1</code> and <code>b2</code>, where each of the two values is
* between <code>0</code> and <code>255</code>, inclusive, then the
* result is equal to:
* <ul><code>
* (short)((b1 << 8) | b2)
* </code></ul>
* <p>
* This method blocks until the two bytes are read, the end of the
* stream is detected, or an exception is thrown.
*
* @return the next two bytes of this file, interpreted as a signed
* 16-bit number.
* @exception EOFException if this file reaches the end before reading
* two bytes.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
public final short readShort() throws IOException {
int ch1 = this.read();
int ch2 = this.read();
if ((ch1 | ch2) < 0)
throw new EOFException();
return (short)((ch1 << 8) + (ch2 << 0));
}
/**
* Reads an unsigned 16-bit number from this file. This method reads
* two bytes from the file. If the bytes read, in order, are
* <code>b1</code> and <code>b2</code>, where
* <code>0 <= b1, b2 <= 255</code>,
* then the result is equal to:
* <ul><code>
* (b1 << 8) | b2
* </code></ul>
* <p>
* This method blocks until the two bytes are read, the end of the
* stream is detected, or an exception is thrown.
*
* @return the next two bytes of this file, interpreted as an unsigned
* 16-bit integer.
* @exception EOFException if this file reaches the end before reading
* two bytes.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
public final int readUnsignedShort() throws IOException {
int ch1 = this.read();
int ch2 = this.read();
if ((ch1 | ch2) < 0)
throw new EOFException();
return (ch1 << 8) + (ch2 << 0);
}
/**
* Reads a Unicode character from this file. This method reads two
* bytes from the file. If the bytes read, in order, are
* <code>b1</code> and <code>b2</code>, where
* <code>0 <= b1, b2 <= 255</code>,
* then the result is equal to:
* <ul><code>
* (char)((b1 << 8) | b2)
* </code></ul>
* <p>
* This method blocks until the two bytes are read, the end of the
* stream is detected, or an exception is thrown.
*
* @return the next two bytes of this file as a Unicode character.
* @exception EOFException if this file reaches the end before reading
* two bytes.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
public final char readChar() throws IOException {
int ch1 = this.read();
int ch2 = this.read();
if ((ch1 | ch2) < 0)
throw new EOFException();
return (char)((ch1 << 8) + (ch2 << 0));
}
/**
* Reads a signed 32-bit integer from this file. This method reads 4
* bytes from the file. If the bytes read, in order, are <code>b1</code>,
* <code>b2</code>, <code>b3</code>, and <code>b4</code>, where
* <code>0 <= b1, b2, b3, b4 <= 255</code>,
* then the result is equal to:
* <ul><code>
* (b1 << 24) | (b2 << 16) + (b3 << 8) + b4
* </code></ul>
* <p>
* This method blocks until the four bytes are read, the end of the
* stream is detected, or an exception is thrown.
*
* @return the next four bytes of this file, interpreted as an
* <code>int</code>.
* @exception EOFException if this file reaches the end before reading
* four bytes.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
public final int readInt() throws IOException {
int ch1 = this.read();
int ch2 = this.read();
int ch3 = this.read();
int ch4 = this.read();
if ((ch1 | ch2 | ch3 | ch4) < 0)
throw new EOFException();
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}
/**
* Reads a signed 64-bit integer from this file. This method reads eight
* bytes from the file. If the bytes read, in order, are
* <code>b1</code>, <code>b2</code>, <code>b3</code>,
* <code>b4</code>, <code>b5</code>, <code>b6</code>,
* <code>b7</code>, and <code>b8,</code> where:
* <ul><code>
* 0 <= b1, b2, b3, b4, b5, b6, b7, b8 <=255,
* </code></ul>
* <p>
* then the result is equal to:
* <p><blockquote><pre>
* ((long)b1 << 56) + ((long)b2 << 48)
* + ((long)b3 << 40) + ((long)b4 << 32)
* + ((long)b5 << 24) + ((long)b6 << 16)
* + ((long)b7 << 8) + b8
* </pre></blockquote>
* <p>
* This method blocks until the eight bytes are read, the end of the
* stream is detected, or an exception is thrown.
*
* @return the next eight bytes of this file, interpreted as a
* <code>long</code>.
* @exception EOFException if this file reaches the end before reading
* eight bytes.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
public final long readLong() throws IOException {
return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
}
/**
* Reads a <code>float</code> from this file. This method reads an
* <code>int</code> value as if by the <code>readInt</code> method
* and then converts that <code>int</code> to a <code>float</code>
* using the <code>intBitsToFloat</code> method in class
* <code>Float</code>.
* <p>
* This method blocks until the four bytes are read, the end of the
* stream is detected, or an exception is thrown.
*
* @return the next four bytes of this file, interpreted as a
* <code>float</code>.
* @exception EOFException if this file reaches the end before reading
* four bytes.
* @exception IOException if an I/O error occurs.
* @see java.io.RandomAccessFile#readInt()
* @see java.lang.Float#intBitsToFloat(int)
* @since JDK1.0
*/
public final float readFloat() throws IOException {
return Float.intBitsToFloat(readInt());
}
/**
* Reads a <code>double</code> from this file. This method reads a
* <code>long</code> value as if by the <code>readLong</code> method
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -