- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
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
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.
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
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).