⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 randomaccessfile.java

📁 This is a resource based on j2me embedded,if you dont understand,you can connection with me .
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * @(#)RandomAccessFile.java	1.75 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER   *    * This program is free software; you can redistribute it and/or   * modify it under the terms of the GNU General Public License version   * 2 only, as published by the Free Software Foundation.    *    * This program is distributed in the hope that it will be useful, but   * WITHOUT ANY WARRANTY; without even the implied warranty of   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * General Public License version 2 for more details (a copy is   * included at /legal/license.txt).    *    * You should have received a copy of the GNU General Public License   * version 2 along with this work; if not, write to the Free Software   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA   * 02110-1301 USA    *    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa   * Clara, CA 95054 or visit www.sun.com if you need additional   * information or have any questions.  * */package java.io;/** * Instances of this class support both reading and writing to a  * random access file. A random access file behaves like a large  * array of bytes stored in the file system. There is a kind of cursor,  * or index into the implied array, called the <em>file pointer</em>;  * input operations read bytes starting at the file pointer and advance  * the file pointer past the bytes read. If the random access file is  * created in read/write mode, then output operations are also available;  * output operations write bytes starting at the file pointer and advance  * the file pointer past the bytes written. Output operations that write  * past the current end of the implied array cause the array to be  * extended. The file pointer can be read by the  * <code>getFilePointer</code> method and set by the <code>seek</code>  * method.  * <p> * It is generally true of all the reading routines in this class that  * if end-of-file is reached before the desired number of bytes has been  * read, an <code>EOFException</code> (which is a kind of  * <code>IOException</code>) is thrown. If any byte cannot be read for  * any reason other than end-of-file, an <code>IOException</code> other  * than <code>EOFException</code> is thrown. In particular, an  * <code>IOException</code> may be thrown if the stream has been closed. * * @author  unascribed * @version 1.57, 05/03/00 * @since   JDK1.0 */public class RandomAccessFile implements DataOutput, DataInput {    private FileDescriptor fd;    private boolean rw;    private static final int O_RDONLY = 1;    private static final int O_RDWR =   2;    private static final int O_SYNC =   4;    private static final int O_DSYNC =  8;    /**     * Creates a random access file stream to read from, and optionally      * to write to, a file with the specified name. A new      * {@link FileDescriptor} object is created to represent the      * connection to the file.     *      * <p> The <tt>mode</tt> argument specifies the access mode with which the     * file is to be opened.  The permitted values and their meanings are as     * specified for the <a     * href="#mode"><tt>RandomAccessFile(File,String)</tt></a> constructor.     *     * <p>     * If there is a security manager, its <code>checkRead</code> method     * is called with the <code>name</code> argument     * as its argument to see if read access to the file is allowed.     * If the mode allows writing, the security manager's     * <code>checkWrite</code> method     * is also called with the <code>name</code> argument     * as its argument to see if write access to the file is allowed.     *     * @param      name   the system-dependent filename     * @param      mode   the access <a href="#mode">mode</a>     * @exception  IllegalArgumentException  if the mode argument is not equal     *               to one of <tt>"r"</tt>, <tt>"rw"</tt>, <tt>"rws"</tt>, or     *               <tt>"rwd"</tt>     * @exception  FileNotFoundException  if the file exists but is a directory     *                   rather than a regular file, or cannot be opened or     *                   created for any other reason     * @exception  SecurityException         if a security manager exists and its     *               <code>checkRead</code> method denies read access to the file     *               or the mode is "rw" and the security manager's     *               <code>checkWrite</code> method denies write access to the file     * @see        java.lang.SecurityException     * @see        java.lang.SecurityManager#checkRead(java.lang.String)     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)     * @revised 1.4     * @spec JSR-51     */    public RandomAccessFile(String name, String mode)	throws FileNotFoundException    {        this(name != null ? new File(name) : null, mode);    }    /**     * Creates a random access file stream to read from, and optionally to     * write to, the file specified by the {@link File} argument.  A new {@link     * FileDescriptor} object is created to represent this file connection.     *     * <a name="mode"><p> The <tt>mode</tt> argument specifies the access mode     * in which the file is to be opened.  The permitted values and their     * meanings are:     *     * <blockquote><table summary="Access mode permitted values and meanings">     * <tr><th><p align="left">Value</p></th><th><p align="left">Meaning</p></th></tr>     * <tr><td valign="top"><tt>"r"</tt></td>     *     <td> Open for reading only.  Invoking any of the <tt>write</tt>     *     methods of the resulting object will cause an {@link     *     java.io.IOException} to be thrown. </td></tr>     * <tr><td valign="top"><tt>"rw"</tt></td>     *     <td> Open for reading and writing.  If the file does not already     *     exist then an attempt will be made to create it. </td></tr>     * <tr><td valign="top"><tt>"rws"</tt></td>     *     <td> Open for reading and writing, as with <tt>"rw"</tt>, and also     *     require that every update to the file's content or metadata be     *     written synchronously to the underlying storage device.  </td></tr>     * <tr><td valign="top"><tt>"rwd"&nbsp;&nbsp;</tt></td>     *     <td> Open for reading and writing, as with <tt>"rw"</tt>, and also     *     require that every update to the file's content be written     *     synchronously to the underlying storage device. </td></tr>     * </table></blockquote>     *     * If     * the file resides on a local storage device then when an invocation of a     * method of this class returns it is guaranteed that all changes made to     * the file by that invocation will have been written to that device.  This     * is useful for ensuring that critical information is not lost in the     * event of a system crash.  If the file does not reside on a local device     * then no such guarantee is made.     *     * <p> The <tt>"rwd"</tt> mode can be used to reduce the number of I/O     * operations performed.  Using <tt>"rwd"</tt> only requires updates to the     * file's content to be written to storage; using <tt>"rws"</tt> requires     * updates to both the file's content and its metadata to be written, which     * generally requires at least one more low-level I/O operation.     *     * <p> If there is a security manager, its <code>checkRead</code> method is     * called with the pathname of the <code>file</code> argument as its     * argument to see if read access to the file is allowed.  If the mode     * allows writing, the security manager's <code>checkWrite</code> method is     * also called with the path argument to see if write access to the file is     * allowed.     *     * @param      file   the file object     * @param      mode   the access mode, as described     *                    <a href="#mode">above</a>     * @exception  IllegalArgumentException  if the mode argument is not equal     *               to one of <tt>"r"</tt>, <tt>"rw"</tt>, <tt>"rws"</tt>, or     *               <tt>"rwd"</tt>     * @exception  FileNotFoundException  if the file exists but is a directory     *                   rather than a regular file, or cannot be opened or     *                   created for any other reason     * @exception  SecurityException         if a security manager exists and its     *               <code>checkRead</code> method denies read access to the file     *               or the mode is "rw" and the security manager's     *               <code>checkWrite</code> method denies write access to the file     * @see        java.lang.SecurityManager#checkRead(java.lang.String)     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)     * @revised 1.4     * @spec JSR-51     */    public RandomAccessFile(File file, String mode)	throws FileNotFoundException    {	String name = (file != null ? file.getPath() : null);	int imode = -1;	if (mode.equals("r"))	    imode = O_RDONLY;	else if (mode.startsWith("rw")) {	    imode = O_RDWR;	    rw = true;	    if (mode.length() > 2) {		if (mode.equals("rws"))		    imode |= O_SYNC;		else if (mode.equals("rwd"))		    imode |= O_DSYNC;		else		    imode = -1;	    }	}	if (imode < 0)	    throw new IllegalArgumentException("Illegal mode \"" + mode					       + "\" must be one of "					       + "\"r\", \"rw\", \"rws\","					       + " or \"rwd\"");	SecurityManager security = System.getSecurityManager();	if (security != null) {	    security.checkRead(name);	    if (rw) {		security.checkWrite(name);	    }	}        if (name == null) {            throw new NullPointerException();        }	fd = new FileDescriptor();	open(name, imode);    }    /**     * Returns the opaque file descriptor object associated with this     * stream. </p>     *     * @return     the file descriptor object associated with this stream.     * @exception  IOException  if an I/O error occurs.     * @see        java.io.FileDescriptor     */    public final FileDescriptor getFD() throws IOException {	if (fd != null) return fd;	throw new IOException();    }    /**     * Returns the unique {@link java.nio.channels.FileChannel FileChannel}     * object associated with this file.     *     * <p> The {@link java.nio.channels.FileChannel#position()     * </code>position<code>} of the returned channel will always be equal to     * this object's file-pointer offset as returned by the {@link     * #getFilePointer getFilePointer} method.  Changing this object's     * file-pointer offset, whether explicitly or by reading or writing bytes,     * will change the position of the channel, and vice versa.  Changing the     * file's length via this object will change the length seen via the file     * channel, and vice versa.     *     * @return  the file channel associated with this file     *     * @since 1.4     * @spec JSR-51     */    /* NOTE: No NIO in CDC    public final FileChannel getChannel() {	synchronized (this) {	    if (channel == null)		channel = FileChannelImpl.open(fd, true, rw, this);	    return channel;	}    }    */     /**     * Opens a file and returns the file descriptor.  The file is      * opened in read-write mode if writeable is true, else      * the file is opened as read-only.     * If the <code>name</code> refers to a directory, an IOException     * is thrown.     *     * @param name the name of the file     * @param mode the mode flags, a combination of the O_ constants     *             defined above     */    private native void open(String name, int mode)	throws FileNotFoundException;    // 'Read' primitives    /**     * Reads a byte of data from this file. The byte is returned as an      * integer in the range 0 to 255 (<code>0x00-0x0ff</code>). This      * method blocks if no input is yet available.      * <p>     * Although <code>RandomAccessFile</code> is not a subclass of      * <code>InputStream</code>, this method behaves in exactly the same      * way as the {@link InputStream#read()} method of      * <code>InputStream</code>.     *     * @return     the next byte of data, or <code>-1</code> if the end of the     *             file has been reached.     * @exception  IOException  if an I/O error occurs. Not thrown if       *                          end-of-file has been reached.     */    public native int read() throws IOException;    /**     * Reads 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 int readBytes(byte b[], int off, int len) throws IOException;    /**     * Reads up to <code>len</code> bytes of data from this file into an      * array of bytes. This method blocks until at least one byte of input      * is available.      * <p>     * Although <code>RandomAccessFile</code> is not a subclass of      * <code>InputStream</code>, this method behaves in the exactly the      * same way as the {@link InputStream#read(byte[], int, int)} method of      * <code>InputStream</code>.     *     * @param      b     the buffer into which the data is read.     * @param      off   the start offset of the data.     * @param      len   the maximum number of bytes read.     * @return     the total number of bytes read into the buffer, or     *             <code>-1</code> if there is no more data because the end of     *             the file has been reached.     * @exception  IOException  if an I/O error occurs.     */    public int read(byte b[], int off, int len) throws IOException {	return readBytes(b, off, len);    }    /**     * Reads up to <code>b.length</code> bytes of data from this file      * into an array of bytes. This method blocks until at least one byte      * of input is available.      * <p>     * Although <code>RandomAccessFile</code> is not a subclass of      * <code>InputStream</code>, this method behaves in the exactly the      * same way as the {@link InputStream#read(byte[])} method of      * <code>InputStream</code>.     *     * @param      b   the buffer into which the data is read.     * @return     the total number of bytes read into the buffer, or     *             <code>-1</code> if there is no more data because the end of     *             this file has been reached.     * @exception  IOException  if an I/O error occurs.     */    public int read(byte b[]) throws IOException {	return readBytes(b, 0, b.length);    }    /**     * Reads <code>b.length</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.     * @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[]) throws IOException {	readFully(b, 0, b.length);    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -