Java File Descriptor Example
In this post we will discuss Java FileDescriptor
class and its use case.
Before we see an example, first lets try to understand what is File Descriptor and why it is used.
Whenever a file is opened, the operating system creates an entry to represent this file and stores its information. Each entry is represented by an integer value and this entry is termed as file descriptor.
Basically, Java class FileDescriptor
provides a handle to the underlying machine-specific structure representing an open file, an open socket, or another source or sink of bytes.
The applications should not create FileDescriptor objects, they are mainly used in creation of FileInputStream
or FileOutputStream
objects to contain it.
So, whenever we create a FileInputStream
or FileOutputStream
object we get a FileDescriptor
object associated with it, we can also use this FileDescriptor
object to create another FileInputStream
or FileOutputStream
object.
Now, Lets see the example of FileInputStream
with FileDescriptor
.
JavaFileDescriptorExample
package com.javacodegeeks.example; import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * * @author anirudh * */ public class JavaFileDescriptorExample { public static void main(String[] args) throws IOException { FileInputStream fileInputStream = new FileInputStream(new File( "/Users/anirudh/test.txt")); FileDescriptor fd = fileInputStream.getFD(); System.out.println(" File descriptor of the file /Users/anirudh/test.txt : " + fd.hashCode()); fileInputStream.close(); FileOutputStream fileOutputStream = new FileOutputStream(new File( "/Users/anirudh/test2.txt")); FileDescriptor fd2 = fileOutputStream.getFD(); System.out.println(" File descriptor of the file /Users/anirudh/test2.txt : " + fd2.hashCode()); fileOutputStream.close(); } }
Output :
File descriptor of the file /Users/anirudh/test.txt : 1534619972 File descriptor of the file /Users/anirudh/test2.txt : 1079268934
We can also use fileDescriptor of one FileInput/OutputStream object to create another similar FileInput/OutputStream object.
Lets see how to do that in an example :
JavaFileDescriptorExample
package com.javacodegeeks.example; import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * * @author anirudh * */ public class JavaFileDescriptorExample { public static void main(String[] args) throws IOException { FileInputStream fileInputStream = new FileInputStream(new File( "/Users/anirudh/test.txt")); FileDescriptor fd = fileInputStream.getFD(); System.out.println(" File descriptor of the file /Users/anirudh/test.txt : " + fd.hashCode()); //Making another FileInput Stream object with file descriptor FileInputStream anotFileInputStream = new FileInputStream(fd); //check value of file descriptor System.out.println("Value of File Desciptor : "+anotFileInputStream.getFD().hashCode()); //See the value of file int i=0; while((i=anotFileInputStream.read())!=-1){ System.out.print((char)i); } fileInputStream.close(); anotFileInputStream.close(); } }
The content of the file test.txt are :
hello Java Code Geeks!
Output :
File descriptor of the file /Users/anirudh/test.txt : 837503310 Value of File Desciptor : 837503310 hello Java Code Geeks!
As we saw in the above example the file descriptor value remained the same for the new file created from same file descriptor.
Download the source code
This was an example where we saw the use of Java class FileDescriptor
.
You can download the full source code of this example here : JavaFileDescriptorExample.zip