📄 randomaccessfile.java
字号:
* 0 through 5 of the second byte. (The second byte should have * 10 as its high order bits). These values are in most significant * byte first (i.e., "big endian") order. * <p> * As an example, if <code>byte1</code> and <code>byte2</code> * are the first two bytes * read respectively, and the high order bits of them match the patterns * which indicate a two byte character encoding, then they would be * converted to a Java <code>char</code> like so: * <p> * <code>(char)(((byte1 & 0x1F) << 6) | (byte2 & 0x3F))</code> * <p> * If the first byte has a <code>1110</code> as its high order bits, then the * character consists of three bytes. The bits that make up the character * value are in positions 0 through 3 of the first byte and bit positions * 0 through 5 of the other two bytes. (The second and third bytes should * have <code>10</code> as their high order bits). These values are in most * significant byte first (i.e., "big endian") order. * <p> * As an example, if <code>byte1</code> <code>byte2</code> * and <code>byte3</code> are the * three bytes read, and the high order bits of them match the patterns * which indicate a three byte character encoding, then they would be * converted to a Java <code>char</code> like so: * <p> * <code>(char)(((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | * (byte3 & 0x3F))</code> * <p> * Note that all characters are encoded in the method that requires the * fewest number of bytes with the exception of the character with the * value of <code>\u0000</code> which is encoded as two bytes. This is * a modification of the UTF standard used to prevent C language style * <code>NUL</code> values from appearing in the byte stream. * <p> * This method can read data that was written by an object implementing the * <code>writeUTF()</code> method in <code>DataOutput</code> * * @return The <code>String</code> read * * @exception EOFException If end of file is reached before reading the * String * @exception UTFDataFormatException If the data is not in UTF-8 format * @exception IOException If any other error occurs * * @see DataOutput */ public final String readUTF () throws IOException { return in.readUTF(); } /** * This method sets the current file position to the specified offset * from the beginning of the file. Note that some operating systems will * allow the file pointer to be set past the current end of the file. * * @param pos The offset from the beginning of the file at which to set * the file pointer * * @exception IOException If an error occurs */ public void seek (long pos) throws IOException { ch.position(pos); } /** * This method attempts to skip and discard the specified number of bytes * in the input stream. It may actually skip fewer bytes than requested. * The actual number of bytes skipped is returned. This method will not * skip any bytes if passed a negative number of bytes to skip. * * @param numBytes The requested number of bytes to skip. * * @return The number of bytes actually skipped. * * @exception IOException If an error occurs. */ public int skipBytes (int numBytes) throws IOException { if (numBytes < 0) throw new IllegalArgumentException ("Can't skip negative bytes: " + numBytes); if (numBytes == 0) return 0; long oldPos = ch.position(); long newPos = oldPos + numBytes; long size = ch.size(); if (newPos > size) newPos = size; ch.position(newPos); return (int) (ch.position() - oldPos); } /** * This method writes a single byte of data to the file. The file must * be open for read-write in order for this operation to succeed. * * @param oneByte The byte of data to write, passed as an int. * * @exception IOException If an error occurs */ public void write (int oneByte) throws IOException { out.write(oneByte); } /** * This method writes all the bytes in the specified array to the file. * The file must be open read-write in order for this operation to succeed. * * @param buffer The array of bytes to write to the file */ public void write (byte[] buffer) throws IOException { out.write(buffer); } /** * This method writes <code>len</code> bytes to the file from the specified * array starting at index <code>offset</code> into the array. * * @param buffer The array of bytes to write to the file * @param offset The index into the array to start writing file * @param len The number of bytes to write * * @exception IOException If an error occurs */ public void write (byte[] buffer, int offset, int len) throws IOException { out.write (buffer, offset, len); } /** * This method writes a Java <code>boolean</code> to the underlying output * stream. For a value of <code>true</code>, 1 is written to the stream. * For a value of <code>false</code>, 0 is written. * * @param val The <code>boolean</code> value to write to the stream * * @exception IOException If an error occurs */ public final void writeBoolean (boolean val) throws IOException { out.writeBoolean(val); } /** * This method writes a Java <code>byte</code> value to the underlying * output stream. * * @param val The <code>byte</code> to write to the stream, passed * as an <code>int</code>. * * @exception IOException If an error occurs */ public final void writeByte (int val) throws IOException { out.writeByte(val); } /** * This method writes a Java <code>short</code> to the stream, high byte * first. This method requires two bytes to encode the value. * * @param val The <code>short</code> value to write to the stream, * passed as an <code>int</code>. * * @exception IOException If an error occurs */ public final void writeShort (int val) throws IOException { out.writeShort(val); } /** * This method writes a single <code>char</code> value to the stream, * high byte first. * * @param val The <code>char</code> value to write, passed as * an <code>int</code>. * * @exception IOException If an error occurs */ public final void writeChar (int val) throws IOException { out.writeChar(val); } /** * This method writes a Java <code>int</code> to the stream, high bytes * first. This method requires four bytes to encode the value. * * @param val The <code>int</code> value to write to the stream. * * @exception IOException If an error occurs */ public final void writeInt (int val) throws IOException { out.writeInt(val); } /** * This method writes a Java <code>long</code> to the stream, high bytes * first. This method requires eight bytes to encode the value. * * @param val The <code>long</code> value to write to the stream. * * @exception IOException If an error occurs */ public final void writeLong (long val) throws IOException { out.writeLong(val); } /** * This method writes a Java <code>float</code> value to the stream. This * value is written by first calling the method * <code>Float.floatToIntBits</code> * to retrieve an <code>int</code> representing the floating point number, * then writing this <code>int</code> value to the stream exactly the same * as the <code>writeInt()</code> method does. * * @param val The floating point number to write to the stream. * * @exception IOException If an error occurs * * @see #writeInt(int) */ public final void writeFloat (float val) throws IOException { out.writeFloat(val); } /** * This method writes a Java <code>double</code> value to the stream. This * value is written by first calling the method * <code>Double.doubleToLongBits</code> * to retrieve an <code>long</code> representing the floating point number, * then writing this <code>long</code> value to the stream exactly the same * as the <code>writeLong()</code> method does. * * @param val The double precision floating point number to write to the * stream. * * @exception IOException If an error occurs * * @see #writeLong(long) */ public final void writeDouble (double val) throws IOException { out.writeDouble(val); } /** * This method writes all the bytes in a <code>String</code> out to the * stream. One byte is written for each character in the <code>String</code>. * The high eight bits of each character are discarded. * * @param val The <code>String</code> to write to the stream * * @exception IOException If an error occurs */ public final void writeBytes (String val) throws IOException { out.writeBytes(val); } /** * This method writes all the characters in a <code>String</code> to the * stream. There will be two bytes for each character value. The high * byte of the character will be written first. * * @param val The <code>String</code> to write to the stream. * * @exception IOException If an error occurs */ public final void writeChars (String val) throws IOException { out.writeChars(val); } /** * This method writes a Java <code>String</code> to the stream in a modified * UTF-8 format. First, two bytes are written to the stream indicating the * number of bytes to follow. Note that this is the number of bytes in the * encoded <code>String</code> not the <code>String</code> length. Next * come the encoded characters. Each character in the <code>String</code> * is encoded as either one, two or three bytes. For characters in the * range of <code>\u0001</code> to <code>\u007F</code>, * one byte is used. The character * value goes into bits 0-7 and bit eight is 0. For characters in the range * of <code>\u0080</code> to <code>\u007FF</code>, two * bytes are used. Bits * 6-10 of the character value are encoded bits 0-4 of the first byte, with * the high bytes having a value of "110". Bits 0-5 of the character value * are stored in bits 0-5 of the second byte, with the high bits set to * "10". This type of encoding is also done for the null character * <code>\u0000</code>. This eliminates any C style NUL character values * in the output. All remaining characters are stored as three bytes. * Bits 12-15 of the character value are stored in bits 0-3 of the first * byte. The high bits of the first bytes are set to "1110". Bits 6-11 * of the character value are stored in bits 0-5 of the second byte. The * high bits of the second byte are set to "10". And bits 0-5 of the * character value are stored in bits 0-5 of byte three, with the high bits * of that byte set to "10". * * @param val The <code>String</code> to write to the output in UTF format * * @exception IOException If an error occurs */ public final void writeUTF (String val) throws IOException { out.writeUTF(val); } /** * This method creates a java.nio.channels.FileChannel. * Nio does not allow one to create a file channel directly. * A file channel must be created by first creating an instance of * Input/Output/RandomAccessFile and invoking the getChannel() method on it. */ public final synchronized FileChannel getChannel () { return ch; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -