Last Updated : 23 May, 2019
Java.io.RandomAccessFileClass 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, CloseableMethods of RandomAccessFile Class :
Syntax : public int read() Parameters : -------- Return : reads byte of data from file, -1 if end of file is reached.
Syntax : 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.
Syntax : 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.
Syntax : public final boolean readBoolean() Parameters : ------ Return : boolean value
Syntax : public final byte readByte() Parameters : ------- Return : signed eight-bit value from file
Syntax : public final char readChar() Parameters : ------- Return : character from the file.
Syntax : public final double readDouble() Parameters : ------ Return : reads a double value from the file.
Syntax : public final double readFloat() Parameters : ------ Return : reads a float value from the file.
Syntax : 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
Syntax : reads a signed 4 bytes integer from the file Parameters : ----- Return : reads a signed 4 bytes integer from the file
Syntax : public final void readFully(byte[] b, int offset, int len) Parameters : b : buffer to read offset : starting position to read len : max no of bytes to read Return : bytes initialising from offset position upto b.length from the buffer
Syntax : 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 main(String[] args)
{
try
{
double d = 1.5;
float f = 14.56f;
// Creating a new RandomAccessFile - "GEEK"
RandomAccessFile geek = new RandomAccessFile("GEEK.txt", "rw");
// Writing to file
geek.writeUTF("Hello Geeks For Geeks");
// File Pointer at index position - 0
geek.seek(0);
// read() method :
System.out.println("Use of read() method : " + geek.read());
geek.seek(0);
byte[] b = {1, 2, 3};
// Use of .read(byte[] b) method :
System.out.println("Use of .read(byte[] b) : " + geek.read(b));
// readBoolean() method :
System.out.println("Use of readBoolean() : " + geek.readBoolean());
// readByte() method :
System.out.println("Use of readByte() : " + geek.readByte());
geek.writeChar('c');
geek.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() : Geeks For Geeks Use of readFully(byte[] b, int off, int len) : Geeks For GeeksNext: Set 2, Set 3
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4