Home » Core Java » io » FileDescriptor » Java File Descriptor Example

About Anirudh Bhatnagar

Anirudh Bhatnagar
Anirudh is a Java programmer with extensive experience in building Java/J2EE applications. He has always been fascinated by the new technologies and emerging trends in software development. He has been involved in propagating these changes and new technologies in his projects. He is an avid blogger and agile enthusiast who believes in writing clean and well tested code.

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.

Download
You can download the full source code of this example here : JavaFileDescriptorExample.zip
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of