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

📄 randomaccessfile.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* RandomAccessFile.java -- Class supporting random file I/O   Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004, 2005  Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version. GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.io;import gnu.java.nio.channels.FileChannelImpl;import java.nio.channels.FileChannel;/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 * "The Java Language Specification", ISBN 0-201-63451-1 * Status: Believe complete and correct to 1.1. *//** * This class allows reading and writing of files at random locations. * Most Java I/O classes are either pure sequential input or output.  This * class fulfills the need to be able to read the bytes of a file in an * arbitrary order.  In addition, this class implements the * <code>DataInput</code> and <code>DataOutput</code> interfaces to allow * the reading and writing of Java primitives. * * @author Aaron M. Renn (arenn@urbanophile.com) * @author Tom Tromey (tromey@cygnus.com) */public class RandomAccessFile implements DataOutput, DataInput{  // The underlying file.  private FileChannelImpl ch;  private FileDescriptor fd;  // The corresponding input and output streams.  private DataOutputStream out;  private DataInputStream in;      /**   * This method initializes a new instance of <code>RandomAccessFile</code>   * to read from the specified <code>File</code> object with the specified    * access mode.   The access mode is either "r" for read only access or "rw"    * for read-write access.   * <p>   * Note that a <code>SecurityManager</code> check is made prior to   * opening the file to determine whether or not this file is allowed to   * be read or written.   *   * @param file The <code>File</code> object to read and/or write.   * @param mode "r" for read only or "rw" for read-write access to the file   *   * @exception IllegalArgumentException If <code>mode</code> has an    * illegal value   * @exception SecurityException If the requested access to the file    * is not allowed   * @exception FileNotFoundException If the file is a directory, or    * any other error occurs   */  public RandomAccessFile (File file, String mode)    throws FileNotFoundException  {    int fdmode;    if (mode.equals("r"))      fdmode = FileChannelImpl.READ;    else if (mode.equals("rw"))      fdmode = FileChannelImpl.READ | FileChannelImpl.WRITE;    else if (mode.equals("rws"))      {	fdmode = (FileChannelImpl.READ | FileChannelImpl.WRITE		  | FileChannelImpl.SYNC);      }    else if (mode.equals("rwd"))      {	fdmode = (FileChannelImpl.READ | FileChannelImpl.WRITE		  | FileChannelImpl.DSYNC);      }    else      throw new IllegalArgumentException ("invalid mode: " + mode);    final String fileName = file.getPath();    // The obligatory SecurityManager stuff    SecurityManager s = System.getSecurityManager();    if (s != null)      {        s.checkRead(fileName);        if ((fdmode & FileChannelImpl.WRITE) != 0)          s.checkWrite(fileName);      }    ch = new FileChannelImpl (file, fdmode);    fd = new FileDescriptor(ch);    out = new DataOutputStream (new FileOutputStream (fd));    in = new DataInputStream (new FileInputStream (fd));  }  /**   * This method initializes a new instance of <code>RandomAccessFile</code>   * to read from the specified file name with the specified access mode.   * The access mode is either "r" for read only access, "rw" for read   * write access, "rws" for synchronized read/write access of both   * content and metadata, or "rwd" for read/write access   * where only content is required to be synchronous.   * <p>   * Note that a <code>SecurityManager</code> check is made prior to   * opening the file to determine whether or not this file is allowed to   * be read or written.   *   * @param fileName The name of the file to read and/or write   * @param mode "r", "rw", "rws", or "rwd"   *   * @exception IllegalArgumentException If <code>mode</code> has an    * illegal value   * @exception SecurityException If the requested access to the file    * is not allowed   * @exception FileNotFoundException If the file is a directory or    * any other error occurs   */  public RandomAccessFile (String fileName, String mode)    throws FileNotFoundException  {    this (new File(fileName), mode);  }  /**   * This method closes the file and frees up all file related system   * resources.  Since most operating systems put a limit on how many files   * may be opened at any given time, it is a good idea to close all files   * when no longer needed to avoid hitting this limit   */  public void close () throws IOException  {    ch.close();  }  /**   * This method returns a <code>FileDescriptor</code> object that    * represents the native file handle for this file.   *   * @return The <code>FileDescriptor</code> object for this file   *   * @exception IOException If an error occurs   */  public final FileDescriptor getFD () throws IOException  {    synchronized (this)      {	if (fd == null)	  fd = new FileDescriptor (ch);	return fd;      }  }  /**   * This method returns the current offset in the file at which the next   * read or write will occur   *   * @return The current file position   *   * @exception IOException If an error occurs   */  public long getFilePointer () throws IOException  {    return ch.position();  }  /**   * This method sets the length of the file to the specified length.   * If the currently length of the file is longer than the specified   * length, then the file is truncated to the specified length (the   * file position is set to the end of file in this case).  If the   * current length of the file is shorter than the specified length,   * the file is extended with bytes of an undefined value (the file   * position is unchanged in this case).   * <p>   * The file must be open for write access for this operation to succeed.   *   * @param newLen The new length of the file   *   * @exception IOException If an error occurs   */  public void setLength (long newLen) throws IOException  {    // FIXME: Extending a file should probably be done by one method call.    // FileChannel.truncate() can only shrink a file.    // To expand it we need to seek forward and write at least one byte.    if (newLen < length())      ch.truncate (newLen);    else if (newLen > length())      {	long pos = getFilePointer();	seek(newLen - 1);	write(0);	seek(pos);      }  }  /**   * This method returns the length of the file in bytes   *   * @return The length of the file   *   * @exception IOException If an error occurs   */  public long length () throws IOException  {    return ch.size();  }  /**   * This method reads a single byte of data from the file and returns it   * as an integer.   *   * @return The byte read as an int, or -1 if the end of the file was reached.   *   * @exception IOException If an error occurs   */  public int read () throws IOException  {    return in.read();  }  /**   * This method reads bytes from the file into the specified array.  The   * bytes are stored starting at the beginning of the array and up to    * <code>buf.length</code> bytes can be read.   *   * @param buffer The buffer to read bytes from the file into   *   * @return The actual number of bytes read or -1 if end of file   *   * @exception IOException If an error occurs   */  public int read (byte[] buffer) throws IOException  {    return in.read (buffer);  }  /**   * This methods reads up to <code>len</code> bytes from the file into the   * specified array starting at position <code>offset</code> into the array.   *   * @param buffer The array to read the bytes into   * @param offset The index into the array to start storing bytes   * @param len The requested number of bytes to read   *   * @return The actual number of bytes read, or -1 if end of file   *   * @exception IOException If an error occurs   */  public int read (byte[] buffer, int offset, int len) throws IOException  {    return in.read (buffer, offset, len);  }  /**   * This method reads a Java boolean value from an input stream.  It does   * so by reading a single byte of data.  If that byte is zero, then the   * value returned is <code>false</code>  If the byte is non-zero, then   * the value returned is <code>true</code>   * <p>   * This method can read a <code>boolean</code> written by an object    * implementing the   * <code>writeBoolean()</code> method in the <code>DataOutput</code>    * interface.   *   * @return The <code>boolean</code> value read   *   * @exception EOFException If end of file is reached before reading the    * boolean   * @exception IOException If any other error occurs   */  public final boolean readBoolean () throws IOException  {    return in.readBoolean ();  }  /**   * This method reads a Java byte value from an input stream.  The value   * is in the range of -128 to 127.   * <p>   * This method can read a <code>byte</code> written by an object    * implementing the    * <code>writeByte()</code> method in the <code>DataOutput</code> interface.   *   * @return The <code>byte</code> value read   *   * @exception EOFException If end of file is reached before reading the byte   * @exception IOException If any other error occurs   *   * @see DataOutput   */  public final byte readByte () throws IOException  {    return in.readByte ();  }

⌨️ 快捷键说明

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