Home » Java programming language

Java FileInputStream getChannel() Method with Example

FileInputStream Class getChannel() method: Here, we are going to learn about the getChannel() method of FileInputStream Class with its syntax and example.
Submitted by Preeti Jain, on April 01, 2020

FileInputStream Class getChannel() method

  • getChannel() method is available in java.io package.
  • getChannel() method is used to return the distinct FileChannel object linked with this FileInputStream.
  • getChannel() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getChannel() method does not throw an exception at the time of getting channel.

Syntax:

    public FileChannel getChannel();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is FileChannel, it returns the FileChannel connected with this stream.

Example:

// Java program to demonstrate the example 
// of FileChannel getChannel() method 
// of FileInputStream

import java.io.*;
import java.nio.channels.*;

public class GetChannelOfFIS {
 public static void main(String[] args) throws Exception {
  FileInputStream fis_stm = null;
  FileChannel file_ch = null;
  int count = 0;

  try {
   // Instantiates FileInputStream
   fis_stm = new FileInputStream("C:\\includehelp.txt");


   while ((count = fis_stm.read()) != -1) {

    // By using read() method is to read
    // a byte from fis_stm
    count = fis_stm.read();

    // Display corresponding bytes value
    byte b = (byte) count;

    // Display value of b
    System.out.println("fis_stm.read(): " + b);

   }

   // By using getChannel() method is to return
   // FileChannel linked with the stream	

   file_ch = fis_stm.getChannel();
   System.out.println("fis_stm.getChannel(): " + file_ch);


  } catch (Exception ex) {
   System.out.println(ex.toString());

  } finally {
   // with the help of this block is to
   // free all necessary resources linked
   // with the stream
   if (fis_stm != null) {
    fis_stm.close();
    if (file_ch != null) {
     file_ch.close();
    }
   }
  }
 }
}

Output

fis_stm.read(): 4
fis_stm.read(): 97
fis_stm.read(): 97
fis_stm.read(): 8
fis_stm.read(): 111
fis_stm.read(): 108
fis_stm.read(): 33
fis_stm.read(): 33
fis_stm.getChannel(): [email protected]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.