View RSS Feed

My Java Tips

Random Access File

Rate this Entry
by , 11-11-2011 at 06:38 PM (2378 Views)
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 file pointer; 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 getFilePointer method and set by the seek method.

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 EOFException (which is a kind of IOException) is thrown. If any byte cannot be read for any reason other than end-of-file, an IOException other than EOFException is thrown. In particular, an IOException may be thrown if the stream has been closed.

Below is the list of the RandomAccessFile class constructors.

RandomAccessFile(File file, String mode)
Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.

RandomAccessFile(String name, String mode)
Creates a random access file stream to read from, and optionally to write to, a file with the specified name

Below is the list of important methods of the RandomAccessFile class.

void close()
Closes this random access file stream and releases any system resources associated with the stream.

long length()
Returns the length of this file.

int read()
Reads a byte of data from this file.

char readChar()
Reads a character from this file.

double readDouble()
Reads a double from this file.

float readFloat()
Reads a float from this file.

int readInt()
Reads a signed 32-bit integer from this file.

String readLine()
Reads the next line of text from this file.

long readLong()
Reads a signed 64-bit integer from this file.

short readShort()
Reads a signed 16-bit number from this file.

int readUnsignedByte()
Reads an unsigned eight-bit number from this file.

int readUnsignedShort()
Reads an unsigned 16-bit number from this file.

void write(byte[] b)
Writes b.length bytes from the specified byte array to this file, starting at the current file pointer.

void write(int b)
Writes the specified byte to this file.

void writeChar(int v)
Writes a char to the file as a two-byte value, high byte first.

void writeChars(String s)
Writes a string to the file as a sequence of characters.

void writeDouble(double v)
Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the file as an eight-byte quantity, high byte first.

void writeInt(int v)
Writes an int to the file as four bytes, high byte first.

void writeLong(long v)
Writes a long to the file as eight bytes, high byte first.

Example

This program shows the implementation of the RandomAccessFile class. Program takes an input from user to the name of a file and checks if it exists then it writes the specified string using the method writeChars( ); Otherwise it displays the appropriate message as "File does not exist". An IOException may be thrown if the stream has been closed.

Java Code:
import java.io.*;

public class RandomAccessFileDemo {

	public static void main(String[] args) throws IOException {

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		System.out.print("Enter File name : ");
		String str = in.readLine();

		File file = new File(str);
		if (!file.exists()) {

			System.out.println("File does not exist.");

			System.exit(0);

		}

		try {
			//Open the file for both reading and writing
			RandomAccessFile rand = new RandomAccessFile(file, "rw");

			rand.seek(file.length()); //Seek to end of file
			rand.writeBytes("Random Access File Demo"); //Write end of file 

			rand.close();

			System.out.println("Write Successfully");

		}

		catch (IOException e) {

			System.out.println(e.getMessage());

		}

	}

}

Submit "Random Access File" to Facebook Submit "Random Access File" to Digg Submit "Random Access File" to del.icio.us Submit "Random Access File" to StumbleUpon Submit "Random Access File" to Google

Comments