Java.io.FileInputStream.getFD() Method

Advertisements


Description

The java.io.FileInputStream.getFD() returns the object of FileDescriptor that identifies the connection to the actual file in the file system being used by this FileInputStream.

Declaration

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

public final FileDescriptor getFD()

Parameters

  • NA

Return Value

The methods returns the file descriptor object associated with this file input stream

Exception

  • NA

Example

The following example shows the usage of java.io.FileInputStream.getFD() method.

package com.tutorialspoint;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.FileInputStream;

public class FileInputStreamDemo {
   public static void main(String[] args) throws IOException {
      
      FileDescriptor fd = null;
      FileInputStream fis = null;
      boolean bool = false;
      
      try{
         // create new file input stream
         fis = new FileInputStream("C://test.txt");
         
         // get file descriptor
         fd = fis.getFD();
         
         // tests if the file is valid
         bool=fd.valid();
         
         // prints
         System.out.println("Valid file: "+bool);
      }catch(Exception ex){
         // if an I/O error occurs
         ex.printStackTrace();
      }finally{
         
         // releases all system resources from the streams
         if(fis!=null)
            fis.close();
      }
   }
}

Assuming we have a text file c:/test.txt, which has the following content. This file will be used as an input for our example program:

ABCDEF

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

Valid file: true



Advertisements
Advertisements