Monday, October 6, 2025
HomeLanguagesJavaFileInputStream getChannel() Method in Java with Examples

FileInputStream getChannel() Method in Java with Examples

The getChannel() method is a part of Java.io.FileInputStream class. This method will return the unique FileChannel object associated with the file input stream. 

  • A channel obtained from the getChannel() method of the Java.io.FileInputStream instance will be “open for reading.”
  • getChannel() will return the FileChannel object that is associated with “this” FileInputStream instance.
  • The channel position will change whenever we read bytes from this stream.
  • Whenever the channel’s position is changed, the stream’s file position will also get changed.
  • The number of bytes read from the file will determine the initial position of the returned channel.

Syntax:

public FileChannel getChannel()

Parameter: getChannel() method has no parameter.

Return Type: getChannel() method will return a unique FileChannel object.

How to invoke getChannel() method?

Step 1: First, we have to create an instance of  Java.io.FileInputStream class

FileInputStream  fileInputStream =new FileInputStream("tmp.txt");

Step 2: To get the unique FileChannel object associated with this fileInputStream, we will call the getChannel() method

FileChannel fileChannel = fileInputStream.getChannel();

The below program will illustrate the use of the getChannel() method.

Example: Program to get the FileChannel object and then to print the size of the channel’s file

Java




// Java Program to demonstrate the working
// of the FileChannel object and then to
// print the size of the channel's file
 
import java.io.*;
// import FileChannel
import java.nio.channels.FileChannel;
 
class GFG {
    public static void main(String[] args)
    {
 
        try {
            // create instance of FileInputStream class
            // user should change name of the file
            FileInputStream fileInputStream
                = new FileInputStream(
                    "C://neveropen//tmp.txt");
 
            // if the specified file does not exist
            if (fileInputStream == null) {
                System.out.println(
                    "Cannot find the specified file");
                return;
            }
 
            // to get the unique object of FileChannel for
            // this specified fileInputStream
            FileChannel fileChannel
                = fileInputStream.getChannel();
 
            // print the current size of the channel's file
            System.out.println(
                "Current Size of the file is : "
                + fileChannel.size());
        }
        catch (Exception exception) {
            System.out.println(exception.getMessage());
        }
    }
}


Output:

Current size of the file is 48

tmp.txt

Note: The programs might not run in an online IDE. Please use an offline IDE and change the Name of the file

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32338 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6707 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6825 POSTS0 COMMENTS
Ted Musemwa
7089 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6779 POSTS0 COMMENTS