A RetroSearch Logo

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

Search Query:

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

Java - FileInputStream getFD() method

Java - FileInputStream getFD() method Description

The Java FileInputStream getFD() method returns the FileDescriptor object associated with the input stream. A FileDescriptor represents a handle to an open file, which can be used for lower-level file operations.

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

IOException− If any I/O error occurs.

Example - Usage of FileInputStream getFD() method

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

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

Assuming we have a text file test.txt in current directory, 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
Example - Checking if a File Descriptor is Valid

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

FileInputStreamDemo.java
package com.tutorialspoint;

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

public class FileInputStreamDemo {
   public static void main(String[] args) {
      try (FileInputStream fis = new FileInputStream("example.txt")) {
         FileDescriptor fd = fis.getFD(); // Get file descriptor

         if (fd.valid()) {
            System.out.println("The file descriptor is valid.");
         } else {
            System.out.println("The file descriptor is not valid.");
         }

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Output(if example.txt exists and opens correctly)

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

The file descriptor is valid.
Explanation Example - Forcing a File to be Written to Disk

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

FileInputStreamDemo.java
package com.tutorialspoint;

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

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

         FileDescriptor fd = fos.getFD(); // Get file descriptor
         fd.sync(); // Force write data to disk

         System.out.println("Data 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 written and synchronized to disk.
Explanation

java_io_fileinputstream.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