Java FileDescriptor class

Last Updated : 9 Feb 2026

The FileDescriptor class provides a handle to a file, socket, or other input/output resource. It represents a connection to an underlying operating system resource.

What is Java FileDescriptor Class?

The FileDescriptor class belongs to the java.io package and is used to identify an open file, standard input, standard output, or standard error stream.

The predefined handles are:

  • in: Standard input
  • out: Standard output
  • err: Standard error

FileDescriptor Class Declaration

The declaration of the FileDescriptor class is as follows:

Fields of FileDescriptor Class

The FileDescriptor class provides predefined static fields.

ModifierTypeFieldDescription
staticFileDescriptorerrA handle to the standard error stream.
staticFileDescriptorinA handle to the standard input stream.
staticFileDescriptoroutA handle to the standard output stream.

Constructors of FileDescriptor Class

The FileDescriptor class provides one constructor that creates an invalid FileDescriptor object.

The syntax of the constructor is as follows:

Methods of FileDescriptor Class

The FileDescriptor class provides methods to manage file synchronization and validation.

Modifier and TypeMethodDescription
voidsync()It force all system buffers to synchronize with the underlying device.
booleanvalid()It tests if this file descriptor object is valid.

Examples of Java FileDescriptor Class

The following examples show how the FileDescriptor class works in Java.

Example 1: Write Data to File Using FileDescriptor

In this example, data is written to a file and synchronized with the disk using the sync() method.

Output:

Data written and synced successfully

Example 2: Read File Content Using FileDescriptor

In this example, a file is read after ensuring data synchronization.

Output:

0123456789:
File read completed
Record.txt Content
0123456789: