Java - RandomAccessFile getFD() method



Description

The Java RandomAccessFile getFD() returns the opaque file descriptor object associated with this stream.

getFD() method −

  • Returns the FileDescriptor object associated with the RandomAccessFile.

  • A FileDescriptor is a handle to the underlying OS-level file.

  • You can use it to −

    • Check if the file is valid/open using valid()

    • Sync data to disk using sync()

Declaration

Following is the declaration for java.io.RandomAccessFile.getFD() method.

public final FileDescriptor getFD()

Parameters

NA

Return Value

This method returns the file descriptor object associated with this stream.

Exception

IOException − If an I/O error occurs.

Example - Usage of RandomAccessFile getFD() method

The following example shows the usage of RandomAccessFile getFD() method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
   
      try {
         // create a new RandomAccessFile with filename test
         RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");

         // write something in the file
         raf.writeUTF("Hello World");

         // set the file pointer at 0 position
         raf.seek(0);

         // read and print the contents of the file
         System.out.println("" + raf.readUTF());

         // return the file descriptor of the stream
         System.out.println("" + raf.getFD());

         // close the strea and release resources
         raf.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

Assuming we have a text file test.txt in current directory which has the following content. This file will be used as an input for our example program −

ABCDE

Let us compile and run the above program, this will produce the following result −

Hello World
java.io.FileDescriptor@152b6651

Example - Checking if the file descriptor is valid

The following example shows the usage of RandomAccessFile getFD() method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.FileDescriptor;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
      try (RandomAccessFile raf = new RandomAccessFile("example1.txt", "rw")) {
         raf.writeUTF("Hello, FileDescriptor!");

         FileDescriptor fd = raf.getFD();

         if (fd.valid()) {
            System.out.println("FileDescriptor is valid.");
         } else {
            System.out.println("FileDescriptor is not valid.");
         }

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

FileDescriptor is valid.

Explanation

  • A file is opened and written to.

  • getFD() retrieves the underlying FileDescriptor.

  • valid() checks if the descriptor still refers to an open file.

Example - Forcing all data to disk using sync()

The following example shows the usage of RandomAccessFile getFD() method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.FileDescriptor;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
      try (RandomAccessFile raf = new RandomAccessFile("example2.txt", "rw")) {
         raf.writeUTF("Important data to persist.");

         FileDescriptor fd = raf.getFD();

         // Ensure data is flushed to the disk
         fd.sync();
         System.out.println("Data synced to disk successfully.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Data synced to disk successfully.

Explanation

  • The file is opened and data is written.

  • getFD() is used to get the FileDescriptor.

  • sync() forces the OS to flush buffers, ensuring data is physically written to disk.

  • Useful in cases where durability is critical (e.g., databases, logs).

java_io_randomaccessfile.htm