FileChannel

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.

Ilias Tsagklis

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button