23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

public RandomAccessFile(File file, String mode) throws <strong>IO</strong>Exception<br />

<strong>Java</strong> I/O<br />

The first argument to the constructor is the file you want to access. The second argument is<br />

the mode for access. The mode should be either the string literal "r" for read-only access or<br />

the string "rw" for read/write access. <strong>Java</strong> does not support write-only access. For example:<br />

RandomAccessFile raf = new RandomAccessFile("29.html", "r");<br />

An IllegalArgumentException is thrown if anything other than the strings "rw" or "r" is<br />

passed as the second argument to this constructor. A security exception is thrown if the<br />

security manager does not allow the requested file to be read. A security exception is also<br />

thrown if you request read/write access, but only read access is allowed. Security checks are<br />

made only when the object is constructed. It is assumed that the security manger's policy<br />

won't change while the program is running. Finally, an <strong>IO</strong>Exception is thrown if the<br />

operating system doesn't allow the file to be accessed or some other I/O problem occurs.<br />

The getFilePointer() and seek() methods allow you to determine and modify the position<br />

in the file at which reads and writes occur. Attempts to seek (position the file pointer) past the<br />

end of the file just move the file pointer to the end of the file. Attempts to write from the end<br />

of the file extend the file.<br />

public native long getFilePointer() throws <strong>IO</strong>Exception<br />

public native void seek(long pos) throws <strong>IO</strong>Exception<br />

Attempts to read from the end of the file throw an EOFException (a subclass of<br />

<strong>IO</strong>Exception). You can determine the length of the file with the length() method:<br />

public native long length() throws <strong>IO</strong>Exception<br />

The RandomAccessFile class implements both the DataInput and DataOutput interfaces.<br />

Therefore, reads and writes use methods exactly like the methods of the DataInputStream<br />

and DataOutputStream classes, such as read(), readFully(), readBoolean(),<br />

writeBoolean(), and so on.<br />

public native int read() throws <strong>IO</strong>Exception<br />

public int read(byte[] data, int offset, int length) throws <strong>IO</strong>Exception<br />

public int read(byte[] data) throws <strong>IO</strong>Exception<br />

public final void readFully(byte[] data) throws <strong>IO</strong>Exception<br />

public final void readFully(byte[] data, int offset, int length)<br />

throws <strong>IO</strong>Exception<br />

public native void write(int b) throws <strong>IO</strong>Exception<br />

public void write(byte[] data) throws <strong>IO</strong>Exception<br />

public void write(byte[] data, int offset, int length) throws <strong>IO</strong>Exception<br />

public final boolean readBoolean() throws <strong>IO</strong>Exception<br />

public final byte readByte() throws <strong>IO</strong>Exception<br />

public final int readUnsignedByte() throws <strong>IO</strong>Exception<br />

public final short readShort() throws <strong>IO</strong>Exception<br />

public final int readUnsignedShort() throws <strong>IO</strong>Exception<br />

public final char readChar() throws <strong>IO</strong>Exception<br />

public final int readInt() throws <strong>IO</strong>Exception<br />

public final long readLong() throws <strong>IO</strong>Exception<br />

public final float readFloat() throws <strong>IO</strong>Exception<br />

public final double readDouble() throws <strong>IO</strong>Exception<br />

public final String readLine() throws <strong>IO</strong>Exception<br />

303

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!