Flat Preloader Icon

Java.io.RandomAccessFile Class Method 

  • Java.io.RandomAccessFile Class provides a way to random access files using reading and writing operations. It works like an array of byte storted in the File.
  • Declaration :
				
					public class RandomAccessFile
   extends Object
      implements DataOutput, DataInput, Closeable
				
			

Methods of RandomAccessFile Class

  • read() : java.io.RandomAccessFile.read() reads byte of data from file. The byte is returned as an integer in the range 0-255
				
					
public int read()
Parameters : 
--------
Return  :
reads byte of data from file, -1 if 
end of file is reached.
				
			
  • read(byte[] b) java.io.RandomAccessFile.read(byte[] b) reads bytes upto b.length from the buffer.
				
					
public int read(byte[] b)
Parameters : 
b : buffer to be read
Return  :
byte of data from file upto b.length,
-1 if end of file is reached.
				
			
  • read((byte[] b, int offset, int len) : java.io.RandomAccessFile.read((byte[] b, int offset, int len) reads bytes initialising from offset position upto b.length from the buffer.
				
					
public int read(byte[] b, int offset, int len)
Parameters : 
b : buffer to read
offset : starting position to read
len : max no of bytes to read
Return  :
reads bytes initialising from offset position 
upto b.length from the buffer.
				
			
  • readBoolean() : java.io.RandomAccessFile.readBoolean() reads a boolean from the file.
				
					
public final boolean readBoolean()
Parameters : 
------
Return  :
boolean value
				
			
  • readByte() : java.io.RandomAccessFile.readByte() reads a signed eight-bit value from file, start reading from the File Pointer.
				
					
public final byte readByte()
Parameters : 
-------
Return  :
signed eight-bit value from file
				
			
  • readDouble() : java.io.RandomAccessFile.readDouble() reads a double value from the file, start reading from the File Pointer.
				
					
public final double readDouble()
Parameters : 
------
Return  :
reads a double value from the file.
				
			
  • readFloat() : java.io.RandomAccessFile.readFloat() reads a float value from the file, start reading from the File Pointer.
				
					
public final double readFloat()
Parameters : 
------
Return  :
reads a float value from the file.
				
			
  • readFully(byte[] b) : java.io.RandomAccessFile.readFully(byte[] b) reads bytes upto b.length from the buffer, start reading from the File Pointer.
				
					
public final void readFully(byte[] b)
Parameters : 
b : buffer to be read
Return  :
reads bytes initialising from offset position 
upto b.length from the buffer
				
			
  • readLong() : java.io.RandomAccessFile.readLong() reads a signed 64 bit integer from the file, start reading from the File Pointer.
				
					
public final long readLong()
Parameters : 
-------
Return  :
signed 64 bit integer from the file
				
			
				
					// Java Program illustrating use of io
.RandomAccessFile class methods
// read(), read(byte[] b), readBoolean(),
readByte(), readInt()
// readFully(byte[] b,
int off, int len), readFully(), readFloat()
// readChar(), readDouble(),

import java.io.*;
public class NewClass
{
public static void mai(String[] args)
	{
	try
		{
	double d = 1.5;
	float f = 14.56f;

	// Creating a new RandomAccessFile
	-"javaprogramer"RandomAccessFile 
	geek = new 
	("javaprogramer.txt", "rw");
// Writing to file
geek.writeUTF("Hello javaprogramer For
javaprogramer");

// File Pointer at index position - 0
javaprogramer.seek(0);

// read() method :
System.out.println("Use of read() method :
" + javaprogramerk.read());
javaprogramer.seek(0);
byte[] b = {1, 2, 3};
			
// Use of .read(byte[] b) method :
System.out.println("Use of 
.read(byte[] b) : " + javaprogramer.read(b));

// readBoolean() method :
System.out.println("Use of readBoolean() 
:" + geek.readBoolean());
// readByte() method :
System.out.println("Use of readByte() : " 
+ geek.readByte());

javaprogramer.writeChar('c');
javaprogramer.seek(0);

// readChar() :
System.out.println("Use of readChar() 
: "+ geek.readChar());

geek.seek(0);
geek.writeDouble(d);
geek.seek(0);
// read double
System.out.println("Use of readDouble() 
: " + geek.readDouble());

geek.seek(0);
geek.writeFloat(f);
geek.seek(0);

// readFloat() :
System.out.println("Use of readFloat()
: "+ geek.readFloat());
geek.seek(0);
// Create array upto geek.length
byte[] arr = new byte[(int) geek.length()];
// readFully() :
geek.readFully(arr);
String str1 = new String(arr);
System.out.println
("Use of readFully() : " + str1);
geek.seek(0);
			
// readFully(byte[] b, int off, int len) :
geek.readFully(arr, 0, 8);
String str2 = new String(arr);
System.out.println("Use of readFully
(byte[] b, int off,
			int len) : " + str2);
		}
		catch (IOException ex)
		{
			System.out.println
			("Something went Wrong");
			ex.printStackTrace();
		}
	}
}

				
			

Output :

				
					Use of read() method : 0
Use of .read(byte[] b) : 3
Use of readBoolean() : true
Use of readByte() : 108
Use of readChar() : c
Use of readDouble() : 1.5
Use of readFloat() : 14.56
Use of readFully() :javaprogramer 
For javaprogramer
Use of readFully(byte[] b, int off, int len) :
javaprogramer For javaprogramer