Open a readable byte channel for a file. - Java java.nio.channels

Java examples for java.nio.channels:ReadableByteChannel

Description

Open a readable byte channel for a file.

Demo Code


//package com.java2s;
import java.io.File;
import java.io.FileNotFoundException;

import java.io.RandomAccessFile;

import java.nio.channels.ReadableByteChannel;

public class Main {
    public static void main(String[] argv) throws Exception {
        File file = new File("Main.java");
        System.out.println(openReadChannel(file));
    }/*w  ww  .  j  a v  a 2 s .  co m*/

    /**
     * Open a readable byte channel for a file.
     * 
     * @param file
     *            A <code>File</code>.
     * @return A <code>ReadableByteChannel</code>.
     * @throws FileNotFoundException
     */
    public static ReadableByteChannel openReadChannel(final File file)
            throws FileNotFoundException {
        return new RandomAccessFile(file, "r").getChannel();
    }
}

Related Tutorials