Class FileDescriptor in Java

digitization, transformation, binary

Class FileDescriptor in Java

An instance of the FileDescriptor class serves as an opaque (not visible on top) handle to the underlying machine-specific structure representing an open file or an open socket. Applications should not create their own FileDescriptor.

FileDescriptor class is a platform-independent representation of a low-level handle to an open file or an open socket. The static in, out, and err variables are FileDescriptor objects that represent the system standard input, output, and error streams, respectively.

Ads code goes here

The FileDescriptor class encapsulates system-specific handles for files and sockets. Instances of this class can be properly constructed only by native methods of other classes. In other words, we should not be constructing our own file descriptors. Currently, file descriptors are returned by the following methods:

  • DatagramSocketImpl.getFileDescriptor()
  • FileInputStream.getFD()
  • FileOutputStream.getFD()
  • RandomAccessFile.getFD()
  • SocketImpl.getFileDescriptor()

A file descriptor can be used in the constructors for FileInputStream, FileOutputStream,
FileReader, and FileWriter.

There is no public constructor method to create a FileDescriptor object. We can obtain one with the getFD() method of FileInputStream, FileOutputStream, and RandomAccessFile.

The structure of the class FileDescriptor  is given as:

public final class java.io.FileDescriptor extends java.lang.Object{
//member elements
public final static FileDescriptor err;//handles the standard error stream
public final static FileDescriptor in;//handles the standard input stream
public final static FileDescriptor out;//handles the standard output stream
//constructor
public FileDescriptor()//creates a default FileDescriptor object
//methods:
public boolean valid();//this method returns true if the file descriptor object represents a valid,open file or socket,false otherwise.
public native void sync() throws SyncFailedException;
}
 

The details of the class structure are given as follows:

public final static FileDescriptor err;

public final static FileDescriptor err is the file descriptor for standard error.

public final static FileDescriptor in;

public final static FileDescriptor in is the file descriptor for standard input.

public final static FileDescriptor out;

public final static FileDescriptor out is the file descriptor for standard output

public FileDescriptor()//creates a default FileDescriptor object

public FileDescriptor() creates a default FileDescriptor object.

public boolean valid();

public boolean valid(); this method returns true if the file descriptor object represents a valid, open file or socket, false otherwise. So it returns a boolean value that indicates the validity of the file descriptor.

READ  Popular Abstract Interfaces in Java

This method returns true if the FileDescriptor represents a valid, open device; false otherwise.

public native void sync() throws SyncFailedException;

public native void sync() throws SyncFailedException method causes the underlying device to be updated to a current state, which typically involves asking the operating system to flush its buffer.

For example, if this FileDescriptor refers to a file on a physical disk, the disk is physically updated to reflect the current state of the object this descriptor represents. This method allows an application to put a device in a known state, which could be useful for transaction processing.

Apart from these methods, the FileDescriptor class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *