The Wayback Machine - https://web.archive.org/web/20240604024358/https://www.geeksforgeeks.org/java-io-filedescriptor-java/
Open In App

Java.io.FileDescriptor in Java

Last Updated : 30 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

io.FileDescriptor in Java

java.io.FileDescriptor works for opening a file having a specific name. If there is any content present in that file it will first erase all that content and put “Beginning of Process” as the first line. Instances of the file descriptor class serve as an opaque handle to the underlying machine-specific structure representing an open file, an open socket, or another source or sink of bytes. 

  • The main practical use for a file descriptor is to create a FileInputStream or FileOutputStream to contain it.
  • Applications should not create their own file descriptors.

Fields  

  • err: A handle to the standard error stream.
  • in: A handle to the standard input stream.
  • out: A handle to the standard output stream.

Declaration :  

public final class FileDescriptor
   extends Object

Constructors :  

  • FileDescriptor() : constructs a FileDescriptor object

Methods:  

  • sync() : java.io.File.sync() synchronizes all the buffers with the underlying device. When all the modified data of the FileDescriptor have been written to the underlying device, the method returns. 
    Syntax :
public void sync()
Parameters :
-----------
Return : 
void
Exception : 
-> SyncFailedException : This is exception is thrown if there is no guarantee of    
                         synchronization of buffers with the device. 

Java




// Java program explaining the working of sync() method
 
import java.io.*;
 
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        // Initializing a FileDescriptor
        FileDescriptor geek_descriptor = null;
        FileOutputStream geek_out = null;
 
        // HERE I'm writing "GEEKS" in my file
        byte[] buffer = {71,69,69,75,83};
 
        try{
            geek_out = new FileOutputStream("FILE.txt");
 
            // This getFD() method is called before closing the output stream
            geek_descriptor = geek_out.getFD();
 
            // writes byte to file output stream
            geek_out.write(buffer);
 
            // USe of sync() : to sync data to the source file
            geek_descriptor.sync();
            System.out.print("\nUse of Sync Successful ");
 
        }
        catch(Exception except)
        {
            // if in case IO error occurs
            except.printStackTrace();
        }
        finally
        {
            // releases system resources
            if(geek_out!=null)
                geek_out.close();
        }
    }
}


Note : 
You can not see the changes made by this code to “FILE.txt” file mentioned in the code as no such file exists on Online IDE. So, you need to copy the code to your SYSTEM compiler and observe the change to your file 
Whatever may be the content present in the file, it will Sync your file to the device and overwrites the data. Now the content of file “FILE.txt” will be 

GEEKS

Even if no such file exists, it will create that file on its own, sync the file, and will write the content, you mention. 
Output : 

Use of Sync Successful :) 
  • valid() : java.io.File.valid() checks whether the FileDescriptor object is valid or not. 
    Syntax :
public boolean valid()
Parameters :
-----------
Return : 
true : if the FileDescriptor object is valid else, false
Exception : 
-----------

Java




// Java program explaining the working of valid() method
 
import java.io.*;
 
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        // Initializing a FileDescriptor
        FileDescriptor geek_descriptor = null;
        FileInputStream geek_in = null;
 
        try
        {
            geek_in = new FileInputStream("FILE.txt");
 
            // get file descriptor
            geek_descriptor = geek_in.getFD();
 
            boolean check = false;
 
            // Use of valid() : checking the validity of FileDescriptor
            check = geek_descriptor.valid();
 
            System.out.print("FileDescriptor is valid : "+check);
 
        }
        catch(Exception except)
        {
            // if in case IO error occurs
            except.printStackTrace();
        }
        finally
        {
            // releases system resources
            if(geek_in!=null)
                geek_in.close();
        }
    }
}


Note : 
You can not see the changes made by this code to the “FILE.txt” file mentioned in the code as no such file exists on Online IDE. So, you need to copy the code to your SYSTEM compiler and observe the change to your file. This method will check the validity of our FileDescriptor. 
Since. our FileDescriptor in the code was valid so, true is returned

Output : 

FileDescriptor is valid : true

Summary:

  • java.io.File.sync() : synchronizes all the buffers with the underlying device.
  • java.io.File.valid() checks whether the FileDescriptor object is valid or not.

 



Previous Article
Next Article

Similar Reads

Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
How to Convert java.sql.Date to java.util.Date in Java?
If we have the Date object of the SQL package, then we can easily convert it into an util Date object. We need to pass the getTime() method while creating the util Date object. java.util.Date utilDate = new java.util.Date(sqlDate.getTime()); It will give us util Date object. getTime() method Syntax: public long getTime() Parameters: The function do
1 min read
Different Ways to Convert java.util.Date to java.time.LocalDate in Java
Prior to Java 8 for handling time and dates we had Date, Calendar, TimeStamp (java.util) but there were several performance issues as well as some methods and classes were deprecated, so Java 8 introduced some new concepts Date and Time APIs' (also known as Joda time APIs') present in java.time package. Java 7: Date, Calendar, TimeStamp present ins
3 min read
How to Convert java.util.Date to java.sql.Date in Java?
Date class is present in both java.util package and java.sql package. Though the name of the class is the same for both packages, their utilities are different. Date class of java.util package is required when data is required in a java application to do any computation or for other various things, while Date class of java.sql package is used whene
3 min read
Java AWT vs Java Swing vs Java FX
Java's UI frameworks include Java AWT, Java Swing, and JavaFX. This plays a very important role in creating the user experience of Java applications. These frameworks provide a range of tools and components for creating graphical user interfaces (GUIs) that are not only functional but also visually appealing. As a Java developer, selecting the righ
11 min read
Java.io.ObjectInputStream Class in Java | Set 2
Java.io.ObjectInputStream Class in Java | Set 1 Note : Java codes mentioned in this article won't run on Online IDE as the file used in the code doesn't exists online. So, to verify the working of the codes, you can copy them to your System and can run it over there. More Methods of ObjectInputStream Class : defaultReadObject() : java.io.ObjectInpu
6 min read
Java.lang.Class class in Java | Set 1
Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It has no public constructor. Class objects are cons
31 min read
Java.lang.StrictMath class in Java | Set 2
Java.lang.StrictMath Class in Java | Set 1More methods of java.lang.StrictMath class 13. exp() : java.lang.StrictMath.exp(double arg) method returns the Euler’s number raised to the power of double argument. Important cases: Result is NaN, if argument is NaN.Result is +ve infinity, if the argument is +ve infinity.Result is +ve zero, if argument is
6 min read
Java 8 | Consumer Interface in Java with Examples
The Consumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one argument and produces a result. However these kind of functions don't return any value.Hence this functional interface which takes in one generic namely:-
4 min read
Java.util.Objects class in Java
Java 7 has come up with a new class Objects that have 9 static utility methods for operating on objects. These utilities include null-safe methods for computing the hash code of an object, returning a string for an object, and comparing two objects. Using Objects class methods, one can smartly handle NullPointerException and can also show customize
8 min read
Practice Tags :