Java - FileDescriptor Class



Introduction

The Java FileDescriptor class instances serve as an opaque handle to the underlying machine-specific structure representing an open file, an open socket, or another source or sink of bytes. Following are the important points about FileDescriptor −

  • The main practical use for a file descriptor is to create a FileInputStream or FileOutputStream to contain it.

  • Applications should not create their own file descriptors.

Class declaration

Following is the declaration for Java.io.FileDescriptor class −

public final class FileDescriptor
   extends Object

Field

Following are the fields for Java.io.FileDescriptor class −

  • static FileDescriptor err − This is the handle to the standard error stream.

  • static FileDescriptor in − This is the handle to the standard input stream.

  • static FileDescriptor out − This is the handle to the standard output stream.

Class constructors

Sr.No. Constructor & Description
1

FileDescriptor()

This method constructs an (invalid) FileDescriptor object.

Class methods

Sr.No. Method & Description
1 void sync()

This method force all system buffers to synchronize with the underlying device.

2 boolean valid()

This method tests if this file descriptor object is valid.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.Object

Example - Using sync() to Ensure Data is Written to Disk

The following example shows the usage of Java FileDescriptor sync() method.

FileDescriptorDemo.java

package com.tutorialspoint;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileDescriptorDemo {
   public static void main(String[] args) {
      File file = new File("sync_example.txt");

      try (FileOutputStream fos = new FileOutputStream(file)) {
         FileDescriptor fd = fos.getFD(); // Get file descriptor

         // Write data to the file
         fos.write("Hello, this is a test for sync method.".getBytes());

         // Force data to be written to disk
         fd.sync();

         System.out.println("Data successfully written and synced to disk.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Data successfully written and synced to disk.

Explanation

  • A FileOutputStream is created for "sync_example.txt".

  • The getFD() method retrieves the file descriptor (FileDescriptor object).

  • Data is written to the file using fos.write().

  • The sync() method is called on the file descriptor, ensuring that the data is immediately written to disk.

  • The file is automatically closed using try-with-resources.

Example - Checking if a FileDescriptor is Valid

The following example shows the usage of Java FileDescriptor valid() method.

FileDescriptorDemo.java

package com.tutorialspoint;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileDescriptorDemo {
   public static void main(String[] args) {
      try (FileOutputStream fos = new FileOutputStream(new File("valid_example.txt"))) {
         FileDescriptor fd = fos.getFD(); // Get the file descriptor

         // Check if the file descriptor is valid
         System.out.println("Is file descriptor valid? " + fd.valid()); // Expected: true
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Is file descriptor valid? true

Explanation

  • A FileOutputStream is created for "valid_example.txt".

  • The file's descriptor (FileDescriptor) is retrieved using getFD().

  • The valid() method checks if the file descriptor is valid.

  • Since the file is open, valid() returns true.

Advertisements