Home » Core Java » nio » FileChannel » Create Stream from FileChannel

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Create Stream from FileChannel

This is an example of how to create input and output streams to read and write data from/to a file in Java. Doing reads and writes of data using Java NIO Channels implies that you should :

  • Create a File object to encapsulate an actual file in the file system
  • To read data from the file you should create a ReadableByteChannel. To do so you must first create a RandomAccessFile object to encapsulate the file object created above and open it for read operations. Then use the getChannel() API method of the RandomAccessFile object to get the file channel to read data from
  • Lastly construct an InputStream that reads bytes from the given channel providing the ReadableByteChannel created above and using the Java NIO Channels class
  • To write data to the file you should create a WritableByteChannel. To do so you must first create a RandomAccessFile object to encapsulate the file object created above and open it for write operations. Then use the getChannel() API method of the RandomAccessFile object to get the file channel to write data to
  • Lastly construct an OutputStream that writes bytes to the given channel providing the WritableByteChannel created above and using the Java NIO Channels class

as described in the code snippet(s) below.

 
Do not forget to close the input and output streams after you are done processing the file so as to release operating system resources.

package com.javacodegeeks.snippets.core;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

public class CreateStreamFromFileChannel {
	
	public static void main(String[] args) {
		
		try {
			
		    File inFile = new File("in.dat");
			
			// Create a readable file channel
		    ReadableByteChannel rChannel = new RandomAccessFile(inFile, "r").getChannel();

		    // Construct a stream that reads bytes from the given channel.
		    InputStream is = Channels.newInputStream(rChannel);
		    
		    File outFile = new File("out.dat");
		 
		    // Create a writable file channel
		    WritableByteChannel wChannel = new RandomAccessFile(outFile, "w").getChannel();

		    // Construct a stream that writes bytes to the given channel.
		    OutputStream os = Channels.newOutputStream(wChannel);

		    // close the channels
		    is.close();
		    os.close();
		    
		}
		catch (IOException e) {
			System.out.println("I/O Error: " + e.getMessage());
		}
		
	}

}

This was an example of how to create a stream from a file channel in Java.

(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