The java.io.FileDescriptor class represents an underlying operating-system resource associated with an open file, socket, or other I/O resource. It provides a low-level connection between Java I/O streams and native system resources.
- Used internally by Java I/O classes to represent open I/O resources.
- Provides methods to check whether a file descriptor is valid and to synchronize buffered data with the underlying device.
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("demo.txt");
FileDescriptor fd = fos.getFD();
System.out.println("Is FileDescriptor valid? " + fd.valid());
fos.close();
}
}
Output
Is FileDescriptor valid? true
Explanation: The FileDescriptor object is obtained from a FileOutputStream and valid() method checks whether the descriptor is connected to an open file. Since the file is open, the output is true.
Class Declaration
public final class FileDescriptor extends Object
- The main practical use for a file descriptor is to create a FileInputStream or FileOutputStream to contain it.
- Applications should not create their own file descriptors.
Constructor
Creates a new, empty file descriptor object that represents an underlying operating system file handle.
Syntax:
FileDescriptor fd = new FileDescriptor();
Common FileDescriptor Objects
- FileDescriptor.in: Represents the standard input (keyboard)
- FileDescriptor.out: Represents the standard output (console)
- FileDescriptor.err: Represents the standard error (error output)
Methods
The FileDescriptor class provides the following commonly used methods:
- void sync(): Forces buffered data associated with the file descriptor to be written to the underlying storage device.
- boolean valid(): Checks whether the FileDescriptor object is valid. It returns true if the descriptor represents an open file, socket, or another active I/O resource; otherwise, it returns false.
Method Example
1. sync() Method
Ensures that all buffered data is written to the underlying storage device.This method forces synchronization between system buffers and the physical device.
Syntax:
public void sync()
- Return: void
- Exception: SyncFailedException - This is exception is thrown if there is no guarantee of synchronization of buffers with the device.
import java.io.*;
public class FileDescriptorSync {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("syncDemo.txt");
FileDescriptor fd = fos.getFD();
fos.write("GeeksForGeeks FileDescriptor Example".getBytes());
fd.sync(); // Ensures data is written to disk
System.out.println("Data synchronized successfully.");
fos.close();
}
}
Output
Is FileDescriptor valid? true Is FileDescriptor valid after close? false
2. valid() Method
Checks whether the FileDescriptor object is currently valid. It returns true if the descriptor is associated with an open file or socket.
Syntax:
public boolean valid()
Return: true if the FileDescriptor object is valid else, false
import java.io.*;
public class FileDescriptorValidity {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("example.txt");
FileDescriptor fd = fos.getFD();
System.out.println("Is FileDescriptor valid? " + fd.valid());
fos.close();
System.out.println("Is FileDescriptor valid after close? " + fd.valid());
}
}
Output
Data synchronized successfully.
Example: with Standard FileDescriptors
import java.io.*;
public class StandardFileDescriptors {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream(FileDescriptor.in);
FileOutputStream fos = new FileOutputStream(FileDescriptor.out);
FileOutputStream fes = new FileOutputStream(FileDescriptor.err);
fos.write("Enter something: ".getBytes());
int data = fis.read();
fes.write(("You entered: " + (char)data + "\n").getBytes());
fis.close();
fos.close();
fes.close();
}
}
Output:
Enter something: A
You entered: A