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.

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

Since file descriptors are only associated with open files and sockets, they become invalid as<br />

soon as the file or socket is closed. You can test whether a file descriptor is still valid with the<br />

valid() method:<br />

public native boolean valid()<br />

This returns true if the descriptor is still valid, false if it isn't.<br />

The one real use to which a client programmer can put a file descriptor object is to sync a file.<br />

This is accomplished with the aptly named sync() method:<br />

public native void sync() throws SyncFailedException<br />

The sync() method forces the system buffers to write all the data they contain to the actual<br />

hardware. Generally, you'll want to flush the stream before syncing it. Flushing clears out<br />

<strong>Java</strong>'s internal buffers. Syncing clears out the operating system's, device driver's, and<br />

hardware's buffers. If synchronization does not succeed, then sync() throws a<br />

java.io.SyncFailedException, a subclass of <strong>IO</strong>Exception.<br />

12.7 Random-Access Files<br />

File input and output streams require you to start reading or writing at the beginning of a file<br />

and then read or write the file in order, possibly skipping over some bytes or backing up but<br />

more or less moving from start to finish. Sometimes, however, you need to read parts of a file<br />

in a more or less random order, where the data near the beginning of the file isn't necessarily<br />

read before the data nearer the end. Other times you need to both read and write the same file.<br />

For example, in record-oriented applications like databases, the actual data may be indexed;<br />

you would use the index to determine where in the file to find the record you need to read or<br />

write. While you could do this by constantly opening and closing the file and skipping to the<br />

point where you needed to read, this is far from efficient. Writes are even worse, since you<br />

would need to read and rewrite the entire file, even to change just one byte of data.<br />

Random-access files can be read from or written to or both from a particular byte position in<br />

the file. A single random-access file can be both read and written without first being closed.<br />

The position in the file where reads and writes start from is indicated by an integer called the<br />

file pointer. Each read or write advances the file pointer by the number of bytes read or<br />

written. Furthermore, the programmer can reposition the file pointer at different bytes in the<br />

file without closing the file.<br />

In <strong>Java</strong>, random file access is performed through the java.io.RandomAccessFile class. This<br />

is not a subclass of java.io.File:<br />

public class RandomAccessFile extends Object implements DataInput,<br />

DataOutput<br />

Among other differences between File objects and RandomAccessFile objects, the<br />

RandomAccessFile constructors actually open the file in question and throw an <strong>IO</strong>Exception<br />

if it doesn't exist:<br />

public RandomAccessFile(String filename, String mode) throws<br />

FileNotFoundException<br />

302

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

Saved successfully!

Ooh no, something went wrong!