📄 randomaccessfile.java
字号:
/** * Reads exactly <code>len</code> bytes from this file into the byte * array, starting at the current file pointer. This method reads * repeatedly from the file until the requested number of bytes are * read. This method blocks until the requested number of bytes are * read, the end of the stream is detected, or an exception is thrown. * * @param b the buffer into which the data is read. * @param off the start offset of the data. * @param len the number of bytes to read. * @exception EOFException if this file reaches the end before reading * all the bytes. * @exception IOException if an I/O error occurs. */ public final void readFully(byte b[], int off, int len) throws IOException { int n = 0; do { int count = this.read(b, off + n, len - n); if (count < 0) throw new EOFException(); n += count; } while (n < len); } /** * Attempts to skip over <code>n</code> bytes of input discarding the * skipped bytes. * <p> * * This method may skip over some smaller number of bytes, possibly zero. * This may result from any of a number of conditions; reaching end of * file before <code>n</code> bytes have been skipped is only one * possibility. This method never throws an <code>EOFException</code>. * The actual number of bytes skipped is returned. If <code>n</code> * is negative, no bytes are skipped. * * @param n the number of bytes to be skipped. * @return the actual number of bytes skipped. * @exception IOException if an I/O error occurs. */ public int skipBytes(int n) throws IOException { long pos; long len; long newpos; if (n <= 0) { return 0; } pos = getFilePointer(); len = length(); newpos = pos + n; if (newpos > len) { newpos = len; } seek(newpos); /* return the actual number of bytes skipped */ return (int) (newpos - pos); } // 'Write' primitives /** * Writes the specified byte to this file. The write starts at * the current file pointer. * * @param b the <code>byte</code> to be written. * @exception IOException if an I/O error occurs. */ public native void write(int b) throws IOException; /** * Writes a sub array as a sequence of bytes. * @param b the data to be written * @param off the start offset in the data * @param len the number of bytes that are written * @exception IOException If an I/O error has occurred. */ private native void writeBytes(byte b[], int off, int len) throws IOException; /** * Writes <code>b.length</code> bytes from the specified byte array * to this file, starting at the current file pointer. * * @param b the data. * @exception IOException if an I/O error occurs. */ public void write(byte b[]) throws IOException { writeBytes(b, 0, b.length); } /** * Writes <code>len</code> bytes from the specified byte array * starting at offset <code>off</code> to this file. * * @param b the data. * @param off the start offset in the data. * @param len the number of bytes to write. * @exception IOException if an I/O error occurs. */ 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. */ 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 <code>pos</code> is less than * <code>0</code> or if an I/O error occurs. */ public native void seek(long pos) throws IOException; /** * Returns the length of this file. * * @return the length of this file, measured in bytes. * @exception IOException if an I/O error occurs. */ public native long length() throws IOException; /** * Sets the length of this file. * * <p> If the present length of the file as returned by the * <code>length</code> method is greater than the <code>newLength</code> * argument then the file will be truncated. In this case, if the file * offset as returned by the <code>getFilePointer</code> method is greater * then <code>newLength</code> then after this method returns the offset * will be equal to <code>newLength</code>. * * <p> If the present length of the file as returned by the * <code>length</code> method is smaller than the <code>newLength</code> * argument then the file will be extended. In this case, the contents of * the extended portion of the file are not defined. * * @param newLength The desired length of the file * @exception IOException If an I/O error occurs * @since 1.2 */ public native void setLength(long newLength) throws IOException; /** * Closes this random access file stream and releases any system * resources associated with the stream. A closed random access * file cannot perform input or output operations and cannot be * reopened. * * @exception IOException if an I/O error occurs. * * @revised 1.4 * @spec JSR-51 */ public void close() throws IOException { close0(); } // // 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, starting at the current file pointer. * 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. */ public final boolean readBoolean() throws IOException { int ch = this.read(); if (ch < 0) throw new EOFException(); return (ch != 0); } /** * Reads a signed eight-bit value from this file. This method reads a * byte from the file, starting from the current file pointer. * If the byte read is <code>b</code>, where * <code>0 <= b <= 255</code>, * then the result is: * <blockquote><pre> * (byte)(b) * </pre></blockquote> * <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 eight-bit * <code>byte</code>. * @exception EOFException if this file has reached the end. * @exception IOException if an I/O error occurs. */ public final byte readByte() throws IOException { int ch = this.read(); if (ch < 0) throw new EOFException(); return (byte)(ch); } /** * Reads an unsigned eight-bit number from this file. This method reads * a byte from this file, starting at the current file pointer, * 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 * eight-bit number. * @exception EOFException if this file has reached the end. * @exception IOException if an I/O error occurs. */ 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 two * bytes from this file, starting at the current file pointer. * 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: * <blockquote><pre> * (short)((b1 << 8) | b2) * </pre></blockquote> * <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. */ 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, starting at the current file pointer. * 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: * <blockquote><pre> * (b1 << 8) | b2 * </pre></blockquote> * <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. */ 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, starting at the current file pointer. * 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: * <blockquote><pre> * (char)((b1 << 8) | b2) * </pre></blockquote> * <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. */ 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, starting at the current file pointer. * 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: * <blockquote><pre> * (b1 << 24) | (b2 << 16) + (b3 << 8) + b4 * </pre></blockquote> * <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. */ 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));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -