
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
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.