RandomAccessFile.java revision 2362
2362N/A * Copyright (c) 1994, 2007, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * Instances of this class support both reading and writing to a 0N/A * random access file. A random access file behaves like a large 0N/A * array of bytes stored in the file system. There is a kind of cursor, 0N/A * or index into the implied array, called the <em>file pointer</em>; 0N/A * input operations read bytes starting at the file pointer and advance 0N/A * the file pointer past the bytes read. If the random access file is 0N/A * created in read/write mode, then output operations are also available; 0N/A * output operations write bytes starting at the file pointer and advance 0N/A * the file pointer past the bytes written. Output operations that write 0N/A * past the current end of the implied array cause the array to be 0N/A * extended. The file pointer can be read by the 0N/A * <code>getFilePointer</code> method and set by the <code>seek</code> 0N/A * It is generally true of all the reading routines in this class that 0N/A * if end-of-file is reached before the desired number of bytes has been 0N/A * read, an <code>EOFException</code> (which is a kind of 0N/A * <code>IOException</code>) is thrown. If any byte cannot be read for 0N/A * any reason other than end-of-file, an <code>IOException</code> other 0N/A * than <code>EOFException</code> is thrown. In particular, an 0N/A * <code>IOException</code> may be thrown if the stream has been closed. 0N/A * @author unascribed 0N/A * Creates a random access file stream to read from, and optionally 0N/A * to write to, a file with the specified name. A new 0N/A * {@link FileDescriptor} object is created to represent the 0N/A * connection to the file. 0N/A * <p> The <tt>mode</tt> argument specifies the access mode with which the 0N/A * file is to be opened. The permitted values and their meanings are as 0N/A * specified for the <a 0N/A * href="#mode"><tt>RandomAccessFile(File,String)</tt></a> constructor. 0N/A * If there is a security manager, its <code>checkRead</code> method 0N/A * is called with the <code>name</code> argument 0N/A * as its argument to see if read access to the file is allowed. 0N/A * If the mode allows writing, the security manager's 0N/A * <code>checkWrite</code> method 0N/A * is also called with the <code>name</code> argument 0N/A * as its argument to see if write access to the file is allowed. 0N/A * @param name the system-dependent filename 0N/A * @param mode the access <a href="#mode">mode</a> 0N/A * @exception IllegalArgumentException if the mode argument is not equal 0N/A * to one of <tt>"r"</tt>, <tt>"rw"</tt>, <tt>"rws"</tt>, or 0N/A * @exception FileNotFoundException 0N/A * if the mode is <tt>"r"</tt> but the given string does not 0N/A * denote an existing regular file, or if the mode begins with 0N/A * <tt>"rw"</tt> but the given string does not denote an 0N/A * existing, writable regular file and a new regular file of 0N/A * that name cannot be created, or if some other error occurs 0N/A * while opening or creating the file 0N/A * @exception SecurityException if a security manager exists and its 0N/A * <code>checkRead</code> method denies read access to the file 0N/A * or the mode is "rw" and the security manager's 0N/A * <code>checkWrite</code> method denies write access to the file 0N/A * @see java.lang.SecurityException 0N/A * @see java.lang.SecurityManager#checkRead(java.lang.String) 0N/A * @see java.lang.SecurityManager#checkWrite(java.lang.String) 0N/A * Creates a random access file stream to read from, and optionally to 0N/A * write to, the file specified by the {@link File} argument. A new {@link 0N/A * FileDescriptor} object is created to represent this file connection. 0N/A * <a name="mode"><p> The <tt>mode</tt> argument specifies the access mode 0N/A * in which the file is to be opened. The permitted values and their 0N/A * <blockquote><table summary="Access mode permitted values and meanings"> 0N/A * <tr><th><p align="left">Value</p></th><th><p align="left">Meaning</p></th></tr> 0N/A * <tr><td valign="top"><tt>"r"</tt></td> 0N/A * <td> Open for reading only. Invoking any of the <tt>write</tt> 0N/A * methods of the resulting object will cause an {@link 0N/A * java.io.IOException} to be thrown. </td></tr> 0N/A * <tr><td valign="top"><tt>"rw"</tt></td> 0N/A * <td> Open for reading and writing. If the file does not already 0N/A * exist then an attempt will be made to create it. </td></tr> 0N/A * <tr><td valign="top"><tt>"rws"</tt></td> 0N/A * <td> Open for reading and writing, as with <tt>"rw"</tt>, and also 0N/A * require that every update to the file's content or metadata be 0N/A * written synchronously to the underlying storage device. </td></tr> 0N/A * <tr><td valign="top"><tt>"rwd" </tt></td> 0N/A * <td> Open for reading and writing, as with <tt>"rw"</tt>, and also 0N/A * require that every update to the file's content be written 0N/A * synchronously to the underlying storage device. </td></tr> 0N/A * </table></blockquote> 0N/A * The <tt>"rws"</tt> and <tt>"rwd"</tt> modes work much like the {@link 0N/A * java.nio.channels.FileChannel#force(boolean) force(boolean)} method of 0N/A * the {@link java.nio.channels.FileChannel} class, passing arguments of 0N/A * <tt>true</tt> and <tt>false</tt>, respectively, except that they always 0N/A * apply to every I/O operation and are therefore often more efficient. If 0N/A * the file resides on a local storage device then when an invocation of a 0N/A * method of this class returns it is guaranteed that all changes made to 0N/A * the file by that invocation will have been written to that device. This 0N/A * is useful for ensuring that critical information is not lost in the 0N/A * event of a system crash. If the file does not reside on a local device 0N/A * then no such guarantee is made. 0N/A * <p> The <tt>"rwd"</tt> mode can be used to reduce the number of I/O 0N/A * operations performed. Using <tt>"rwd"</tt> only requires updates to the 0N/A * file's content to be written to storage; using <tt>"rws"</tt> requires 0N/A * updates to both the file's content and its metadata to be written, which 0N/A * generally requires at least one more low-level I/O operation. 0N/A * <p> If there is a security manager, its <code>checkRead</code> method is 0N/A * called with the pathname of the <code>file</code> argument as its 0N/A * argument to see if read access to the file is allowed. If the mode 0N/A * allows writing, the security manager's <code>checkWrite</code> method is 0N/A * also called with the path argument to see if write access to the file is 0N/A * @param file the file object 0N/A * @param mode the access mode, as described 0N/A * <a href="#mode">above</a> 0N/A * @exception IllegalArgumentException if the mode argument is not equal 0N/A * to one of <tt>"r"</tt>, <tt>"rw"</tt>, <tt>"rws"</tt>, or 0N/A * @exception FileNotFoundException 0N/A * if the mode is <tt>"r"</tt> but the given file object does 0N/A * not denote an existing regular file, or if the mode begins 0N/A * with <tt>"rw"</tt> but the given file object does not denote 0N/A * an existing, writable regular file and a new regular file of 0N/A * that name cannot be created, or if some other error occurs 0N/A * while opening or creating the file 0N/A * @exception SecurityException if a security manager exists and its 0N/A * <code>checkRead</code> method denies read access to the file 0N/A * or the mode is "rw" and the security manager's 0N/A * <code>checkWrite</code> method denies write access to the file 0N/A * @see java.lang.SecurityManager#checkRead(java.lang.String) 0N/A * @see java.lang.SecurityManager#checkWrite(java.lang.String) 0N/A * @see java.nio.channels.FileChannel#force(boolean) 0N/A +
"\" must be one of " 0N/A +
"\"r\", \"rw\", \"rws\"," 0N/A * Returns the opaque file descriptor object associated with this 0N/A * @return the file descriptor object associated with this stream. 0N/A * @exception IOException if an I/O error occurs. 0N/A * @see java.io.FileDescriptor 0N/A * Returns the unique {@link java.nio.channels.FileChannel FileChannel} 0N/A * object associated with this file. 0N/A * <p> The {@link java.nio.channels.FileChannel#position() 0N/A * </code>position<code>} of the returned channel will always be equal to 0N/A * this object's file-pointer offset as returned by the {@link 0N/A * #getFilePointer getFilePointer} method. Changing this object's 0N/A * file-pointer offset, whether explicitly or by reading or writing bytes, 0N/A * will change the position of the channel, and vice versa. Changing the 0N/A * file's length via this object will change the length seen via the file 0N/A * channel, and vice versa. 0N/A * @return the file channel associated with this file 0N/A synchronized (
this) {
0N/A * FileDescriptor could be shared by FileInputStream or 0N/A * are done using it. 0N/A * Increment fd's use count. Invoking the channel's close() 0N/A * method will result in decrementing the use count set for 0N/A * Opens a file and returns the file descriptor. The file is 0N/A * opened in read-write mode if the O_RDWR bit in <code>mode</code> 0N/A * is true, else the file is opened as read-only. 0N/A * If the <code>name</code> refers to a directory, an IOException 0N/A * @param name the name of the file 0N/A * @param mode the mode flags, a combination of the O_ constants 0N/A // 'Read' primitives 0N/A * Reads a byte of data from this file. The byte is returned as an 0N/A * integer in the range 0 to 255 (<code>0x00-0x0ff</code>). This 0N/A * method blocks if no input is yet available. 0N/A * Although <code>RandomAccessFile</code> is not a subclass of 0N/A * <code>InputStream</code>, this method behaves in exactly the same 0N/A * way as the {@link InputStream#read()} method of 0N/A * <code>InputStream</code>. 0N/A * @return the next byte of data, or <code>-1</code> if the end of the 0N/A * file has been reached. 0N/A * @exception IOException if an I/O error occurs. Not thrown if 0N/A * end-of-file has been reached. 0N/A * Reads a sub array as a sequence of bytes. 0N/A * @param b the buffer into which the data is read. 0N/A * @param off the start offset of the data. 0N/A * @param len the number of bytes to read. 0N/A * @exception IOException If an I/O error has occurred. 0N/A * Reads up to <code>len</code> bytes of data from this file into an 0N/A * array of bytes. This method blocks until at least one byte of input 0N/A * Although <code>RandomAccessFile</code> is not a subclass of 0N/A * <code>InputStream</code>, this method behaves in exactly the 0N/A * same way as the {@link InputStream#read(byte[], int, int)} method of 0N/A * <code>InputStream</code>. 0N/A * @param b the buffer into which the data is read. 0N/A * @param off the start offset in array <code>b</code> 0N/A * at which the data is written. 0N/A * @param len the maximum number of bytes read. 0N/A * @return the total number of bytes read into the buffer, or 0N/A * <code>-1</code> if there is no more data because the end of 0N/A * the file has been reached. 0N/A * @exception IOException If the first byte cannot be read for any reason 0N/A * other than end of file, or if the random access file has been closed, or if 0N/A * some other I/O error occurs. 0N/A * @exception NullPointerException If <code>b</code> is <code>null</code>. 0N/A * @exception IndexOutOfBoundsException If <code>off</code> is negative, 0N/A * <code>len</code> is negative, or <code>len</code> is greater than 0N/A * <code>b.length - off</code> 0N/A * Reads up to <code>b.length</code> bytes of data from this file 0N/A * into an array of bytes. This method blocks until at least one byte 0N/A * of input is available. 0N/A * Although <code>RandomAccessFile</code> is not a subclass of 0N/A * <code>InputStream</code>, this method behaves in exactly the 0N/A * same way as the {@link InputStream#read(byte[])} method of 0N/A * <code>InputStream</code>. 0N/A * @param b the buffer into which the data is read. 0N/A * @return the total number of bytes read into the buffer, or 0N/A * <code>-1</code> if there is no more data because the end of 0N/A * this file has been reached. 0N/A * @exception IOException If the first byte cannot be read for any reason 0N/A * other than end of file, or if the random access file has been closed, or if 0N/A * some other I/O error occurs. 0N/A * @exception NullPointerException If <code>b</code> is <code>null</code>. 0N/A * Reads <code>b.length</code> bytes from this file into the byte 0N/A * array, starting at the current file pointer. This method reads 0N/A * repeatedly from the file until the requested number of bytes are 0N/A * read. This method blocks until the requested number of bytes are 0N/A * read, the end of the stream is detected, or an exception is thrown. 0N/A * @param b the buffer into which the data is read. 0N/A * @exception EOFException if this file reaches the end before reading 0N/A * @exception IOException if an I/O error occurs. 0N/A * Reads exactly <code>len</code> bytes from this file into the byte 0N/A * array, starting at the current file pointer. This method reads 0N/A * repeatedly from the file until the requested number of bytes are 0N/A * read. This method blocks until the requested number of bytes are 0N/A * read, the end of the stream is detected, or an exception is thrown. 0N/A * @param b the buffer into which the data is read. 0N/A * @param off the start offset of the data. 0N/A * @param len the number of bytes to read. 0N/A * @exception EOFException if this file reaches the end before reading 0N/A * @exception IOException if an I/O error occurs. 0N/A * Attempts to skip over <code>n</code> bytes of input discarding the 0N/A * This method may skip over some smaller number of bytes, possibly zero. 0N/A * This may result from any of a number of conditions; reaching end of 0N/A * file before <code>n</code> bytes have been skipped is only one 0N/A * possibility. This method never throws an <code>EOFException</code>. 0N/A * The actual number of bytes skipped is returned. If <code>n</code> 0N/A * is negative, no bytes are skipped. 0N/A * @param n the number of bytes to be skipped. 0N/A * @return the actual number of bytes skipped. 0N/A * @exception IOException if an I/O error occurs. 0N/A /* return the actual number of bytes skipped */ 0N/A // 'Write' primitives 0N/A * Writes the specified byte to this file. The write starts at 0N/A * the current file pointer. 0N/A * @param b the <code>byte</code> to be written. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Writes a sub array as a sequence of bytes. 0N/A * @param b the data to be written 0N/A * @param off the start offset in the data 0N/A * @param len the number of bytes that are written 0N/A * @exception IOException If an I/O error has occurred. 0N/A * Writes <code>b.length</code> bytes from the specified byte array 0N/A * to this file, starting at the current file pointer. 0N/A * @param b the data. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Writes <code>len</code> bytes from the specified byte array 0N/A * starting at offset <code>off</code> to this file. 0N/A * @param b the data. 0N/A * @param off the start offset in the data. 0N/A * @param len the number of bytes to write. 0N/A * @exception IOException if an I/O error occurs. 0N/A // 'Random access' stuff 0N/A * Returns the current offset in this file. 0N/A * @return the offset from the beginning of the file, in bytes, 0N/A * at which the next read or write occurs. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Sets the file-pointer offset, measured from the beginning of this 0N/A * file, at which the next read or write occurs. The offset may be 0N/A * set beyond the end of the file. Setting the offset beyond the end 0N/A * of the file does not change the file length. The file length will 0N/A * change only by writing after the offset has been set beyond the end 0N/A * @param pos the offset position, measured in bytes from the 0N/A * beginning of the file, at which to set the file 0N/A * @exception IOException if <code>pos</code> is less than 0N/A * <code>0</code> or if an I/O error occurs. 0N/A * Returns the length of this file. 0N/A * @return the length of this file, measured in bytes. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Sets the length of this file. 0N/A * <p> If the present length of the file as returned by the 0N/A * <code>length</code> method is greater than the <code>newLength</code> 0N/A * argument then the file will be truncated. In this case, if the file 0N/A * offset as returned by the <code>getFilePointer</code> method is greater 0N/A * than <code>newLength</code> then after this method returns the offset 0N/A * will be equal to <code>newLength</code>. 0N/A * <p> If the present length of the file as returned by the 0N/A * <code>length</code> method is smaller than the <code>newLength</code> 0N/A * argument then the file will be extended. In this case, the contents of 0N/A * the extended portion of the file are not defined. 0N/A * @param newLength The desired length of the file 0N/A * @exception IOException If an I/O error occurs 0N/A * Closes this random access file stream and releases any system 0N/A * resources associated with the stream. A closed random access 0N/A * file cannot perform input or output operations and cannot be 0N/A * <p> If this file has an associated channel then the channel is closed 0N/A * @exception IOException if an I/O error occurs. 0N/A * Decrement FD use count associated with the channel. The FD use 0N/A * count is incremented whenever a new channel is obtained from 0N/A * Decrement FD use count associated with this stream. 0N/A * The count got incremented by FileDescriptor during its construction. 0N/A // DataInputStream and DataOutputStream. 0N/A * Reads a <code>boolean</code> from this file. This method reads a 0N/A * single byte from the file, starting at the current file pointer. 0N/A * A value of <code>0</code> represents 0N/A * <code>false</code>. Any other value represents <code>true</code>. 0N/A * This method blocks until the byte is read, the end of the stream 0N/A * is detected, or an exception is thrown. 0N/A * @return the <code>boolean</code> value read. 0N/A * @exception EOFException if this file has reached the end. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Reads a signed eight-bit value from this file. This method reads a 0N/A * byte from the file, starting from the current file pointer. 0N/A * If the byte read is <code>b</code>, where 0N/A * <code>0 <= b <= 255</code>, 0N/A * then the result is: 0N/A * </pre></blockquote> 0N/A * This method blocks until the byte is read, the end of the stream 0N/A * is detected, or an exception is thrown. 0N/A * @return the next byte of this file as a signed eight-bit 0N/A * <code>byte</code>. 0N/A * @exception EOFException if this file has reached the end. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Reads an unsigned eight-bit number from this file. This method reads 0N/A * a byte from this file, starting at the current file pointer, 0N/A * and returns that byte. 0N/A * This method blocks until the byte is read, the end of the stream 0N/A * is detected, or an exception is thrown. 0N/A * @return the next byte of this file, interpreted as an unsigned 0N/A * @exception EOFException if this file has reached the end. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Reads a signed 16-bit number from this file. The method reads two 0N/A * bytes from this file, starting at the current file pointer. 0N/A * If the two bytes read, in order, are 0N/A * <code>b1</code> and <code>b2</code>, where each of the two values is 0N/A * between <code>0</code> and <code>255</code>, inclusive, then the 0N/A * result is equal to: 0N/A * (short)((b1 << 8) | b2) 0N/A * </pre></blockquote> 0N/A * This method blocks until the two bytes are read, the end of the 0N/A * stream is detected, or an exception is thrown. 0N/A * @return the next two bytes of this file, interpreted as a signed 0N/A * @exception EOFException if this file reaches the end before reading 0N/A * @exception IOException if an I/O error occurs. 0N/A * Reads an unsigned 16-bit number from this file. This method reads 0N/A * two bytes from the file, starting at the current file pointer. 0N/A * If the bytes read, in order, are 0N/A * <code>b1</code> and <code>b2</code>, where 0N/A * <code>0 <= b1, b2 <= 255</code>, 0N/A * then the result is equal to: 0N/A * (b1 << 8) | b2 0N/A * </pre></blockquote> 0N/A * This method blocks until the two bytes are read, the end of the 0N/A * stream is detected, or an exception is thrown. 0N/A * @return the next two bytes of this file, interpreted as an unsigned 0N/A * @exception EOFException if this file reaches the end before reading 0N/A * @exception IOException if an I/O error occurs. 0N/A * Reads a character from this file. This method reads two 0N/A * bytes from the file, starting at the current file pointer. 0N/A * If the bytes read, in order, are 0N/A * <code>b1</code> and <code>b2</code>, where 0N/A * <code>0 <= b1, b2 <= 255</code>, 0N/A * then the result is equal to: 0N/A * (char)((b1 << 8) | b2) 0N/A * </pre></blockquote> 0N/A * This method blocks until the two bytes are read, the end of the 0N/A * stream is detected, or an exception is thrown. 0N/A * @return the next two bytes of this file, interpreted as a 0N/A * <code>char</code>. 0N/A * @exception EOFException if this file reaches the end before reading 0N/A * @exception IOException if an I/O error occurs. 0N/A * Reads a signed 32-bit integer from this file. This method reads 4 0N/A * bytes from the file, starting at the current file pointer. 0N/A * If the bytes read, in order, are <code>b1</code>, 0N/A * <code>b2</code>, <code>b3</code>, and <code>b4</code>, where 0N/A * <code>0 <= b1, b2, b3, b4 <= 255</code>, 0N/A * then the result is equal to: 0N/A * (b1 << 24) | (b2 << 16) + (b3 << 8) + b4 0N/A * </pre></blockquote> 0N/A * This method blocks until the four bytes are read, the end of the 0N/A * stream is detected, or an exception is thrown. 0N/A * @return the next four bytes of this file, interpreted as an 0N/A * @exception EOFException if this file reaches the end before reading 0N/A * @exception IOException if an I/O error occurs. 0N/A * Reads a signed 64-bit integer from this file. This method reads eight 0N/A * bytes from the file, starting at the current file pointer. 0N/A * If the bytes read, in order, are 0N/A * <code>b1</code>, <code>b2</code>, <code>b3</code>, 0N/A * <code>b4</code>, <code>b5</code>, <code>b6</code>, 0N/A * <code>b7</code>, and <code>b8,</code> where: 0N/A * 0 <= b1, b2, b3, b4, b5, b6, b7, b8 <=255, 0N/A * </pre></blockquote> 0N/A * then the result is equal to: 0N/A * <p><blockquote><pre> 0N/A * ((long)b1 << 56) + ((long)b2 << 48) 0N/A * + ((long)b3 << 40) + ((long)b4 << 32) 0N/A * + ((long)b5 << 24) + ((long)b6 << 16) 0N/A * + ((long)b7 << 8) + b8 0N/A * </pre></blockquote> 0N/A * This method blocks until the eight bytes are read, the end of the 0N/A * stream is detected, or an exception is thrown. 0N/A * @return the next eight bytes of this file, interpreted as a 0N/A * <code>long</code>. 0N/A * @exception EOFException if this file reaches the end before reading 0N/A * @exception IOException if an I/O error occurs. 0N/A * Reads a <code>float</code> from this file. This method reads an 0N/A * <code>int</code> value, starting at the current file pointer, 0N/A * as if by the <code>readInt</code> method 0N/A * and then converts that <code>int</code> to a <code>float</code> 0N/A * using the <code>intBitsToFloat</code> method in class 0N/A * <code>Float</code>. 0N/A * This method blocks until the four bytes are read, the end of the 0N/A * stream is detected, or an exception is thrown. 0N/A * @return the next four bytes of this file, interpreted as a 0N/A * <code>float</code>. 0N/A * @exception EOFException if this file reaches the end before reading 0N/A * @exception IOException if an I/O error occurs. 0N/A * @see java.io.RandomAccessFile#readInt() 0N/A * @see java.lang.Float#intBitsToFloat(int) 0N/A * Reads a <code>double</code> from this file. This method reads a 0N/A * <code>long</code> value, starting at the current file pointer, 0N/A * as if by the <code>readLong</code> method 0N/A * and then converts that <code>long</code> to a <code>double</code> 0N/A * using the <code>longBitsToDouble</code> method in 0N/A * class <code>Double</code>. 0N/A * This method blocks until the eight bytes are read, the end of the 0N/A * stream is detected, or an exception is thrown. 0N/A * @return the next eight bytes of this file, interpreted as a 0N/A * <code>double</code>. 0N/A * @exception EOFException if this file reaches the end before reading 0N/A * @exception IOException if an I/O error occurs. 0N/A * @see java.io.RandomAccessFile#readLong() 0N/A * @see java.lang.Double#longBitsToDouble(long) 0N/A * Reads the next line of text from this file. This method successively 0N/A * reads bytes from the file, starting at the current file pointer, 0N/A * until it reaches a line terminator or the end 0N/A * of the file. Each byte is converted into a character by taking the 0N/A * byte's value for the lower eight bits of the character and setting the 0N/A * high eight bits of the character to zero. This method does not, 0N/A * therefore, support the full Unicode character set. 0N/A * <p> A line of text is terminated by a carriage-return character 0N/A * (<code>'\r'</code>), a newline character (<code>'\n'</code>), a 0N/A * carriage-return character immediately followed by a newline character, 0N/A * or the end of the file. Line-terminating characters are discarded and 0N/A * are not included as part of the string returned. 0N/A * <p> This method blocks until a newline character is read, a carriage 0N/A * return and the byte following it are read (to see if it is a newline), 0N/A * the end of the file is reached, or an exception is thrown. 0N/A * @return the next line of text from this file, or null if end 0N/A * of file is encountered before even one byte is read. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Reads in a string from this file. The string has been encoded 0N/A * The first two bytes are read, starting from the current file 0N/A * <code>readUnsignedShort</code>. This value gives the number of 0N/A * following bytes that are in the encoded string, not 0N/A * the length of the resulting string. The following bytes are then 0N/A * interpreted as bytes encoding characters in the modified UTF-8 format 0N/A * and are converted into characters. 0N/A * This method blocks until all the bytes are read, the end of the 0N/A * stream is detected, or an exception is thrown. 0N/A * @return a Unicode string. 0N/A * @exception EOFException if this file reaches the end before 0N/A * reading all the bytes. 0N/A * @exception IOException if an I/O error occurs. 0N/A * @exception UTFDataFormatException if the bytes do not represent 0N/A * valid modified UTF-8 encoding of a Unicode string. 0N/A * @see java.io.RandomAccessFile#readUnsignedShort() 0N/A * Writes a <code>boolean</code> to the file as a one-byte value. The 0N/A * value <code>true</code> is written out as the value 0N/A * <code>(byte)1</code>; the value <code>false</code> is written out 0N/A * as the value <code>(byte)0</code>. The write starts at 0N/A * the current position of the file pointer. 0N/A * @param v a <code>boolean</code> value to be written. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Writes a <code>byte</code> to the file as a one-byte value. The 0N/A * write starts at the current position of the file pointer. 0N/A * @param v a <code>byte</code> value to be written. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Writes a <code>short</code> to the file as two bytes, high byte first. 0N/A * The write starts at the current position of the file pointer. 0N/A * @param v a <code>short</code> to be written. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Writes a <code>char</code> to the file as a two-byte value, high 0N/A * byte first. The write starts at the current position of the 0N/A * @param v a <code>char</code> value to be written. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Writes an <code>int</code> to the file as four bytes, high byte first. 0N/A * The write starts at the current position of the file pointer. 0N/A * @param v an <code>int</code> to be written. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Writes a <code>long</code> to the file as eight bytes, high byte first. 0N/A * The write starts at the current position of the file pointer. 0N/A * @param v a <code>long</code> to be written. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Converts the float argument to an <code>int</code> using the 0N/A * <code>floatToIntBits</code> method in class <code>Float</code>, 0N/A * and then writes that <code>int</code> value to the file as a 0N/A * four-byte quantity, high byte first. The write starts at the 0N/A * current position of the file pointer. 0N/A * @param v a <code>float</code> value to be written. 0N/A * @exception IOException if an I/O error occurs. 0N/A * @see java.lang.Float#floatToIntBits(float) 0N/A * Converts the double argument to a <code>long</code> using the 0N/A * <code>doubleToLongBits</code> method in class <code>Double</code>, 0N/A * and then writes that <code>long</code> value to the file as an 0N/A * eight-byte quantity, high byte first. The write starts at the current 0N/A * position of the file pointer. 0N/A * @param v a <code>double</code> value to be written. 0N/A * @exception IOException if an I/O error occurs. 0N/A * @see java.lang.Double#doubleToLongBits(double) 0N/A * Writes the string to the file as a sequence of bytes. Each 0N/A * character in the string is written out, in sequence, by discarding 0N/A * its high eight bits. The write starts at the current position of 0N/A * @param s a string of bytes to be written. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Writes a string to the file as a sequence of characters. Each 0N/A * character is written to the data output stream as if by the 0N/A * <code>writeChar</code> method. The write starts at the current 0N/A * position of the file pointer. 0N/A * @param s a <code>String</code> value to be written. 0N/A * @exception IOException if an I/O error occurs. 0N/A * @see java.io.RandomAccessFile#writeChar(int) 0N/A for (
int i =
0, j =
0; i <
clen; i++) {
0N/A b[j++] = (
byte)(c[i] >>>
8);
0N/A b[j++] = (
byte)(c[i] >>>
0);
0N/A * Writes a string to the file using 0N/A * encoding in a machine-independent manner. 0N/A * First, two bytes are written to the file, starting at the 0N/A * current file pointer, as if by the 0N/A * <code>writeShort</code> method giving the number of bytes to 0N/A * follow. This value is the number of bytes actually written out, 0N/A * not the length of the string. Following the length, each character 0N/A * of the string is output, in sequence, using the modified UTF-8 encoding 0N/A * for each character. 0N/A * @param str a string to be written. 0N/A * @exception IOException if an I/O error occurs.