Java - FileDescriptor valid() method
Description
The Java FileDescriptor valid() method checks whether the file descriptor is valid or invalid. It returns−
true if the file descriptor is active and can be used.
false if the file descriptor is invalid or closed.
Declaration
Following is the declaration for java.io.FileDescriptor.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 - Usage of FileDescriptor valid() method
The following example shows the usage of Java FileDescriptor valid() method.
FileDescriptorDemo.java
package com.tutorialspoint;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
public class FileDescriptorDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis = null;
FileDescriptor fd = null;
boolean bool = false;
try {
// create input stream
fis = new FileInputStream("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();
}
}
}
Output
Let us compile and run the above program, this will produce the following result−
is file descriptor valid?: true
Example - Checking if a FileDescriptor is Valid
The following example shows the usage of Java FileDescriptor valid() method.
FileDescriptorDemo.java
package com.tutorialspoint;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileDescriptorDemo {
public static void main(String[] args) {
try (FileOutputStream fos = new FileOutputStream(new File("valid_example.txt"))) {
FileDescriptor fd = fos.getFD(); // Get the file descriptor
// Check if the file descriptor is valid
System.out.println("Is file descriptor valid? " + fd.valid()); // Expected: true
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
Let us compile and run the above program, this will produce the following result−
Is file descriptor valid? true
Explanation
A FileOutputStream is created for "valid_example.txt".
The file's descriptor (FileDescriptor) is retrieved using getFD().
The valid() method checks if the file descriptor is valid.
Since the file is open, valid() returns true.
Example - Checking Validity After Closing the Stream
The following example shows the usage of Java FileDescriptor valid() method.
FileDescriptorDemo.java
package com.tutorialspoint;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileDescriptorDemo {
public static void main(String[] args) {
FileDescriptor fd = null;
try (FileOutputStream fos = new FileOutputStream(new File("invalid_example.txt"))) {
fd = fos.getFD(); // Get the file descriptor
// Check if the file descriptor is valid before closing
System.out.println("Before closing, is file descriptor valid? " + fd.valid()); // Expected: true
} catch (IOException e) {
e.printStackTrace();
}
// Check if the file descriptor is valid after closing
System.out.println("After closing, is file descriptor valid? " + fd.valid()); // Expected: false
}
}
Output
Let us compile and run the above program, this will produce the following result−
Before closing, is file descriptor valid? True After closing, is file descriptor valid? false
Explanation
A FileOutputStream is created for "invalid_example.txt", and its FileDescriptor is retrieved.
The valid() method checks if the file descriptor is valid before closing the stream (should return true).
The try-with-resources block automatically closes the stream.
After closing the stream, calling valid() again returns false because the file descriptor is no longer in use.