A RetroSearch Logo

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

Search Query:

Showing content from https://www.tutorialspoint.com/java/io/fileoutputstream_getfd.htm below:

Java - FileOutputStream getFD() method

Java - FileOutputStream getFD() method Description

The Java FileOutputStream getFD() returns the file descriptor associated with the file output stream. A file descriptor (FileDescriptor) is a handle that represents an open file, socket, or another system resource.

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 an I/O error occurs.

Example - Usage of FileOutputStream getFD() method

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

FileOutputStreamDemo.java
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("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();
      }
   }
}
Output

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

Is file valid? true
Example - Using getFD() to Ensure Data is Written to Disk

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

FileOutputStreamDemo.java
package com.tutorialspoint;

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

public class FileOutputStreamDemo {
   public static void main(String[] args) {
      try (FileOutputStream fos = new FileOutputStream("output.txt")) {
         // Write data to the file
         fos.write("Hello, FileDescriptor!".getBytes());

         // Get FileDescriptor associated with FileOutputStream
         FileDescriptor fd = fos.getFD();

         // Force synchronization of file data to disk
         fd.sync();

         System.out.println("Data successfully written and synchronized 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 synchronized to disk.
Explanation Example - Checking If a FileDescriptor is Valid

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

FileOutputStreamDemo.java
package com.tutorialspoint;

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

public class FileOutputStreamDemo {
   public static void main(String[] args) {
      try (FileOutputStream fos = new FileOutputStream("testfile.txt")) {
         // Get FileDescriptor
         FileDescriptor fd = fos.getFD();

         // Check if the FileDescriptor is valid
         if (fd.valid()) {
            System.out.println("FileDescriptor is valid. Ready for operations.");
         } else {
            System.out.println("FileDescriptor is not valid.");
         }

         // Write some data
         fos.write("Checking FileDescriptor validity.".getBytes());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Output()

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

FileDescriptor is valid. Ready for operations.
Explanation

java_io_fileoutputstream.htm


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