Java.io.FileOutputStream.getFD() Method



Description

The java.io.FileOutputStream.getFD() method returns the file descriptor associated with this stream.

Declaration

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

public final FileDescriptor getFD()

Parameters

NA

Return Value

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

Exception

IOException − If any I/O error occurs.

Example

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

package com.tutorialspoint;

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

public class FileOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      FileOutputStream fos = null;
      FileDescriptor fd = null;
      boolean bool = false;
            
      try {
         // create new file output stream
         fos = new FileOutputStream("C://test.txt");
         
         // get file descriptor instance
         fd = fos.getFD();
         
         // test if the file is valid
         bool = fd.valid();
      
         // print
         System.out.print("Is file valid? "+bool);
         
      } catch(Exception ex) {
         // if an error occurs
         ex.printStackTrace();
      } finally {
         if(fos!=null)
            fos.close();
      }
   }
}

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

Is file valid? true
java_io_fileoutputstream.htm
Advertisements