Home »
Java programming language
Java RandomAccessFile getFD() Method with Example
RandomAccessFile Class getFD() method: Here, we are going to learn about the getFD() method of RandomAccessFile Class with its syntax and example.
Submitted by Preeti Jain, on March 23, 2020
RandomAccessFile Class getFD() method
- getFD() method is available in java.io package.
- getFD() method is used to retrieve the file descriptor linked with this RandomAccessFile stream.
- getFD() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- getFD() method may throw an exception at the time of returning the file channel.
IOException: This exception may throw while performing input/output operation.
Syntax:
public final FileDescriptor getFD();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of this method is FileDescriptor, it returns FileDescriptor object connected with this stream.
Example:
// Java program to demonstrate the example
// of FileDescription getFD() method of
// RandomAccessFile
import java.io.*;
public class RAFGetFD {
public static void main(String[] args) throws Exception {
// Instantiate a random access file
// object with file name and permissions
RandomAccessFile ra_f = new RandomAccessFile("e:/includehelp.txt", "rw");
// By using writeUTF() method is to
// write data in the file
ra_f.writeUTF("Welcome, in Java World!!!");
// Initially set the file pointer
// is at 0 for reading the file
ra_f.seek(0);
// To read file content by using
// readUTF()
System.out.println("ra_f .readUTF(): " + ra_f.readUTF());
// By using getFD() method is to
// return the file descriptor linked with this
// object
System.out.println("ra_f.getFD(): " + ra_f.getFD());
// By using close() method isto
// close this stream ran_f
ra_f.close();
}
}
Output
ra_f .readUTF(): Welcome, in Java World!!!
ra_f.getFD(): java.io.FileDescriptor@ad3ba4