📄 randomaccessfile.java
字号:
/*
* @(#)RandomAccessFile.java 1.33 98/01/09
*
* Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*
* CopyrightVersion 1.1_beta
*
*/
package java.io;
import java.io.File;
/**
* Instances of this class support both reading and writing to a
* random access file. An application can modify the position in the
* file at which the next read or write occurs.
* This class provides a sense of security
* by offering methods that allow specified mode accesses of
* read-only or read-write to files.
*
* @author unascribed
* @version 1.33, 01/09/98
* @since JDK1.0
*/
public
class RandomAccessFile implements DataOutput, DataInput {
private FileDescriptor fd;
/**
* Creates a random access file stream to read from, and optionally
* to write to, a file with the specified name.
* <p>
* The mode argument must either be equal to <code>"r"</code> or
* <code>"rw"</code>, indicating either to open the file for input or
* for both input and output.
*
* @param name the system-dependent filename.
* @param mode the access mode.
* @exception IllegalArgumentException if the mode argument is not equal
* to <code>"r"</code> or to <code>"rw"</code>.
* @exception IOException if an I/O error occurs.
* @exception SecurityException if a security manager exists, its
* <code>checkRead</code> method is called with the name
* argument to see if the application is allowed read access
* to the file. If the mode argument is equal to
* <code>"rw"</code>, its <code>checkWrite</code> method also
* is called with the name argument to see if the application
* is allowed write access to the file. Either of these may
* result in a security exception.
* @see java.lang.SecurityException
* @see java.lang.SecurityManager#checkRead(java.lang.String)
* @since JDK1.0
*/
public RandomAccessFile(String name, String mode) throws IOException {
boolean rw = mode.equals("rw");
if (!rw && !mode.equals("r"))
throw new IllegalArgumentException("mode must be r or rw");
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(name);
if (rw) {
security.checkWrite(name);
}
}
fd = new FileDescriptor();
open(name, rw);
}
/**
* Creates a random access file stream to read from, and optionally
* to write to, the file specified by the <code>File</code> argument.
* <p>
* The mode argument must either be equal to <code>"r"</code> or to
* <code>"rw"</code>, indicating either to open the file for input,
* or for both input and output, respectively.
*
* @param file the file object.
* @param mode the access mode.
* @exception IllegalArgumentException if the mode argument is not equal
* to <code>"r"</code> or to <code>"rw"</code>.
* @exception IOException if an I/O error occurs.
* @exception SecurityException if a security manager exists, its
* <code>checkRead</code> method is called with the pathname
* of the <code>File</code> argument to see if the
* application is allowed read access to the file. If the
* mode argument is equal to <code>"rw"</code>, its
* <code>checkWrite</code> method also is called with the
* pathname to see if the application is allowed write access
* to the file.
* @see java.io.File#getPath()
* @see java.lang.SecurityManager#checkRead(java.lang.String)
* @since JDK1.0
*/
public RandomAccessFile(File file, String mode) throws IOException {
this(file.getPath(), mode);
}
/**
* Returns the opaque file descriptor object associated with this stream.
*
* @return the file descriptor object associated with this stream.
* @exception IOException if an I/O error occurs.
* @see java.io.FileDescriptor
* @since JDK1.0
*/
public final FileDescriptor getFD() throws IOException {
if (fd != null) return fd;
throw new IOException();
}
/**
* 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.
* @param name the name of the file
* @param writeable the boolean indicating whether file is
* writeable or not.
*/
private native void open(String name, boolean writeable) throws IOException;
// 'Read' primitives
/**
* Reads a byte of data from this file. This method blocks if no
* input is yet available.
*
* @return the next byte of data, or <code>-1</code> if the end of the
* file is reached.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
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.
*
* @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.
* @since JDK1.0
*/
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.
*
* @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.
* @since JDK1.0
*/
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. This method reads repeatedly from the file until all the
* bytes are read. This method blocks until all the 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.
* @since JDK1.0
*/
public final void readFully(byte b[]) throws IOException {
readFully(b, 0, b.length);
}
/**
* Reads exactly <code>len</code> bytes from this file into the byte
* array. This method reads repeatedly from the file until all the
* bytes are read. This method blocks until all the 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.
* @since JDK1.0
*/
public final void readFully(byte b[], int off, int len) throws IOException {
int n = 0;
while (n < len) {
int count = this.read(b, off + n, len - n);
if (count < 0)
throw new EOFException();
n += count;
}
}
/**
* Skips exactly <code>n</code> bytes of input.
* <p>
* This method blocks until all the bytes are skipped, the end of
* the stream is detected, or an exception is thrown.
*
* @param n the number of bytes to be skipped.
* @return the number of bytes skipped, which is always <code>n</code>.
* @exception EOFException if this file reaches the end before skipping
* all the bytes.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
public int skipBytes(int n) throws IOException {
seek(getFilePointer() + n);
return n;
}
// 'Write' primitives
/**
* Writes the specified byte to this file.
*
* @param b the <code>byte</code> to be written.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
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
* starting at offset <code>off</code> to this file.
*
* @param b the data.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
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.
* @since JDK1.0
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -