Java.io.File.valid() Method


Advertisements


Description

The java.io.File.valid() method tests if file descriptor object is valid.

Declaration

Following is the declaration for java.io.File.valid() method −

public boolean valid()

Parameters

NA

Return Value

The method returns true if the file descriptor object is valid, else the method returns false.

Exception

NA

Example

The following example shows the usage of java.io.File.valid() method.

package com.tutorialspoint;

import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;

public class FileDemo {

   public static void main(String[] args) throws IOException {
      FileInputStream fis = null;
      FileDescriptor fd = null;
      boolean bool = false;   
      
      try {
      
         // create input stream
         fis = new FileInputStream("c:/java test.txt");
         
         // get file descriptor
         fd = fis.getFD();
         
         // tests file descriptor object's validity
         bool = fd.valid();
         
         // print
         System.out.print("is file descriptor valid?: "+bool);
         
      } catch(Exception e) {
      
         // if any error occurs
         e.printStackTrace();
      } finally {
      
         // releases systems resources
         if(fis!=null)
            fis.close();   
      }
   }
}

Let us compile and run the above program, this will produce the following result −

is file descriptor valid?: true

java_io_filedescriptor.htm

Advertisements