A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/fileinputstream-getfd-method-in-java-with-examples/ below:

FileInputStream getFD() Method in Java with Examples

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. 

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

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

import java.io.*;
class GFG {
    public static void main(String[] args)
    {
        try {
            // create instance of FileInputStream class
            // user should change name of the file
            FileInputStream fileInputStream
                = new FileInputStream(
                    "C://geeksforgeeks//tmp.txt");

            // if the specified file does not exist
            if (fileInputStream == null) {
                System.out.println(
                    "Cannot find the specified file");
                return;
            }

            // to get the object of FileDescriptor for
            // this specified fileInputStream
            FileDescriptor fileDescriptor
                = fileInputStream.getFD();

            // check if the fileDescriptor is valid or not
            // using it's valid method
            // valid() will return true if valid else false
            System.out.println("Is FileDescriptor valid : "
                               + fileDescriptor.valid());
          
            // will close the file input stream and releases
            // any system resources associated with the
            // stream.
            fileInputStream.close();
        }
        catch (Exception exception) {
            System.out.println(exception.getMessage());
        }
    }
}


Output:

Is FileDescriptor valid : true
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.



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