Home » Java programming language

Java FileDescriptor valid() Method with Example

FileDescriptor Class valid() method: Here, we are going to learn about the valid() method of FileDescriptor Class with its syntax and example.
Submitted by Preeti Jain, on April 02, 2020

FileDescriptor Class valid() method

  • valid() method is available in java.io package.
  • valid() method is used to check whether this FileDescriptor object is valid or not.
  • valid() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • valid() method does not throw an exception at the time of checking the status of an object.

Syntax:

    public boolean valid();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is boolean, it returns true when this FileDescriptor is valid otherwise it returns false.

Example:

// Java program to demonstrate the example 
// of boolean valid() method of FileDescriptor

import java.io.*;

public class ValidOfFD {
 public static void main(String[] args) throws Exception {
  FileInputStream is_stm = null;

  try {
   // Instantiates FileInputStream 
   is_stm = new FileInputStream("D:\\includehelp.txt");

   // By using getFD() method is to get
   // the file descriptor
   FileDescriptor file_des = is_stm.getFD();
   System.out.println("is_stm.getFD(): " + file_des);

   // By using valid() method is to check 
   // whether the file descriptor is valid or
   // not
   boolean status = file_des.valid();
   System.out.println("is_stm.valid(): " + status);
  } catch (Exception ex) {
   System.out.println(ex.toString());
  } finally {
   // with the help of this block is to
   // free all necessary resources linked
   // with the stream
   if (is_stm != null) {
    is_stm.close();
   }
  }
 }
}

Output

is_stm.getFD(): java.io.FileDescriptor@7bfcd12c
is_stm.valid(): true



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.