VOOZH about

URL: https://www.geeksforgeeks.org/java/fileinputstream-getfd-method-in-java-with-examples/

⇱ FileInputStream getFD() Method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

FileInputStream getFD() Method in Java with Examples

Last Updated : 23 Jul, 2025

Java.io.FileInputStream.getFD() method is a part of  Java.io.FileInputStream class. This method will return the FileDescriptor object associated with the file input stream. 

  • getFD() method is declared as final that means getFD() cannot be overridden in the subclasses
  • FileDescriptor object that we will get using getFD() method will represent the connection to the actual file in the file system
  • getFD() method might throw IOException.

Syntax:

public final FileDescriptor getFD() throws IOException

Return Type: getFD() method will return the instance of FileDescriptor associated with this FileInputStream.

Exception: getFD() method might throw IOException if any Input/output exception raises.

How to invoke getFD() method?

Step 1: First, we have to create an instance of Java.io.FileInputStream class

FileInputStream fileInputStream =new FileInputStream("tmp.txt");

Step 2: To get the instance of FileDescriptor associated with this fileInputStream, we will invoke the getFD() method

FileDescriptor fileDescriptor =fileInputStream.getFD();

Example: Java Program to get an instance of FileDescriptor and then to check it is valid or not 

In the below program, we will

  • use  Java.io.FileInputStream.getFD() method to get the object of FileDescriptor
  • use FileDescriptor valid() method to check if the instance of the file descriptor is valid or not


Output:

Is FileDescriptor valid : true
👁 Image
tmp.txt

Note: The programs will run on an online IDE. Please use an offline IDE and change the Name of the file according to your need.

Comment
Article Tags: