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.
DeclarationFollowing is the declaration for java.io.FileInputStream.getFD() method −
public final FileDescriptor getFD()Parameters
NA
Return ValueThe methods returns the file descriptor object associated with this file input stream.
ExceptionIOException− If any I/O error occurs.
Example - Usage of FileInputStream getFD() methodThe following example shows the usage of Java FileInputStream getFD() method.
FileInputStreamDemo.javapackage 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: trueExample - Checking if a File Descriptor is Valid
The following example shows the usage of Java FileInputStream getFD() method.
FileInputStreamDemo.javapackage 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
Open FileInputStream for "example.txt".
Retrieve the file descriptor using getFD().
Check if the file descriptor is valid using fd.valid().
Print whether the file descriptor is valid or not.
The following example shows the usage of Java FileInputStream getFD() method.
FileInputStreamDemo.javapackage 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
Open FileOutputStream for "example.txt".
Write data to the file using write().
Retrieve the file descriptor using getFD().
Call fd.sync() to forcefully flush all written data to disk (ensuring no data loss in case of a system crash).
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