Search This Blog

Total Pageviews

SeekableByteChannel example using FileChannel in Java

SeekableByteChannel: A Channel to read, write, map and, manipulate a file.  It has a current position within its file which can be both queried and modified.

        -- For huge files this is often much more efficient than invoking the usual read or write methods.

        -- Ensures the data is not lost in the event of a system crash.

        -- Very fast transfer directly to or from the filesystem cache.

        -- File channels are safe for use by multiple concurrent threads. Sample Program to use FileChannel to copy a File.
public class CopyFileChannel { public static void main(String str[]) throws IOException { /** Use try with resource feature to autoclose the FileChannel.*/
try (FileChannel inputChannel = new FileInputStream("Fromfile_PATH").getChannel()){                  try (FileChannel outputChannel = new FileOutputStream("Tofile_PATH").getChannel()){                 inputChannel.transferTo(0, inputChannel.size(), outputChannel);
                    }                }                  } }

No comments:

BlockingQueue Applications

Java BlockingQueue is a versatile data structure that can be used in various real-time scenarios where multiple threads need to communicate ...