A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/io/fileinputstream_getchannel.htm below:

Java - FileInputStream getChannel() method

Java - FileInputStream getChannel() method Description

The Java FileInputStream getChannel() method returns a FileChannel object, which provides advanced file operations such as non-blocking I/O, file locking, memory mapping, and more.

Declaration

Following is the declaration for java.io.FileInputStream.getChannel() method −

public FileChannel getChannel()
Parameters

NA

Return Value

The methods returns the channel ssociated with this file input stream.

Exception

IOException− If any I/O error occurs.

Example - Usage of FileInputStream getChannel() method

The following example shows the usage of Java FileInputStream getChannel() method.

FileInputStreamDemo.java
package com.tutorialspoint;

import java.io.IOException;
import java.io.FileInputStream;
import java.nio.channels.FileChannel;

public class FileInputStreamDemo {
   public static void main(String[] args) throws IOException {
      FileChannel fc = null;
      FileInputStream fis = null;
      int i = 0;
      long pos;
      char c;
      
      try {
         // create new file input stream
         fis = new FileInputStream("test.txt");
         
         // read till the end of the file
         while((i = fis.read())!=-1) {
         
            // get file channel
            fc = fis.getChannel();
            
            // get channel position
            pos = fc.position();
            
            // integer to character
            c = (char)i;
            
            // prints
            System.out.print("No of bytes read: "+pos);
            System.out.println("; Char read: "+c);
         }
         
      } catch(Exception ex) {
         // if an I/O error occurs
         System.out.println("IOException: close called before read()");
      } finally {
         // releases all system resources from the streams
         if(fis!=null)
            fis.close();
         if(fc!=null)
            fc.close();
      }
   }
}
Output Assumption

Assuming we have a text file test.txt in current directory, which has the following content. This file will be used as an input for our example program.

ABCDEF

Let us compile and run the above program, this will produce the following result−

No of bytes read: 1; Char read: A
No of bytes read: 2; Char read: B
No of bytes read: 3; Char read: C
No of bytes read: 4; Char read: D
No of bytes read: 5; Char read: E
No of bytes read: 6; Char read: F
Example - Using getChannel() to Read Data from a File

The following example shows the usage of Java FileInputStream getChannel() method.

FileInputStreamDemo.java
package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileInputStreamDemo {
   public static void main(String[] args) {
      try (FileInputStream fis = new FileInputStream("example.txt")) {
         FileChannel channel = fis.getChannel(); // Get FileChannel from FileInputStream

         ByteBuffer buffer = ByteBuffer.allocate(1024); // Allocate buffer
         int bytesRead = channel.read(buffer); // Read data into buffer

         if (bytesRead != -1) {
            buffer.flip(); // Prepare buffer for reading
            while (buffer.hasRemaining()) {
               System.out.print((char) buffer.get()); // Print character by character
            }
         }

         System.out.println("\nFile read successfully using FileChannel.");

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Output(if example.txt contains "Hello")

Let us compile and run the above program, this will produce the following result−

Hello
File read successfully using FileChannel.
Explanation Example - Getting File Size Using FileChannel

The following example shows the usage of Java FileInputStream getChannel() method.

FileInputStreamDemo.java
package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class FileInputStreamDemo {
   public static void main(String[] args) {
      try (FileInputStream fis = new FileInputStream("example.txt")) {
         FileChannel channel = fis.getChannel(); // Get FileChannel

         long fileSize = channel.size(); // Get file size in bytes
         System.out.println("File size: " + fileSize + " bytes");

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Possible Output(if example.txt has 50 characters)

Let us compile and run the above program, this will produce the following result−

File size: 50 bytes
Explanation

java_io_fileinputstream.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4