Java.io - SPLessons

Java.io FileDescriptor

Home > Lesson > Chapter 14
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io FileDescriptor

Java.io FileDescriptor

shape Introduction

Java.io FileDescriptor class is used to access the file or input /output resources. The class instances serve as a machine-specific structure, which represent an open file, socket, another source or sink of bytes. Mainly the file descriptor is used to create a File I/O stream to hold it. Any application could not create their file descriptors by own.

Class Declaration

shape Declaration

Java.io FileDescriptor class is declared as shown below: public final class FileDescriptor extends object

shape Fields

Java.io FileDescriptorclass fields are as follows:
  • static FileDescriptor i: This field is used to hold the quality input stream.
  • static FileDescriptor o: This field is used to hold the quality output stream.
  • static FileDescriptor er: This field is used to hold the quality error stream.

Class Constructors

shape Table

Constructor Description
FileDescriptor() This constructor constructs an Java.io FileDescriptor object (invalid).

Class Methods

shape Table

Method Description
void sync() This method functions with the underlying device all system buffers to synchronize.
boolean valid() This method checks that the file descriptor object is valid or not.

Inherited Methods

shape Description

From the following classes methods are inherited to Java.io FileDescriptor
  • Java.io.Object

shape Examples

Usage of boolean valid() method. [c]import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.IOException; public class FileDescriptor { public static void main(String[] args) throws IOException { FileInputStream fi = null; FileDescriptor fd = null; boolean bl = false; try{ // creating input stream fi = new FileInputStream("c:/java test.txt"); // It gets file descriptor fd = fi.getFD(); // testing the validity of file descriptor object bl = fd.valid(); // print the the file discriptor is valid or not System.out.print("Is file descriptor valid? "+bl); }catch(Exception e) { // if any error occurs e.printStackTrace(); }finally { // Releases all systems resources if(fi!=null) fi.close(); } } }[/c] Output The result is as follows. [c]Is file descriptor is valid? true [/c]

Summary

shape Key Points

  • Java.io.FileDescriptor class is used to access the file or input/output resources.
  • The class instances serve as a machine-specific structure, which represent an open file, socket, another source or sink of bytes.
  • Mainly the file descriptor is used to create a File I/O stream to hold it.
  • Any application could not create their  file descriptors by own.