A RetroSearch Logo

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

Search Query:

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

Java - FileOutputStream getChannel() method

Java - FileOutputStream getChannel() method Description

The Java FileOutputStream getChannel() returns a FileChannel object associated with the output stream. FileChannel is part of the java.nio package and provides more efficient file operations, such as reading and writing data in a non-blocking way. Enables operations like memory-mapped files, random access, and file locking.

Declaration

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

public FileChannel getChannel()
Parameters

NA

Return Value

This method returns the file channel associated with this file output stream.

Exception

IOException− If an I/O error occurs.

Example - Usage of FileOutputStream getChannel() method

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

FileOutputStreamDemo.java
package com.tutorialspoint;

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

public class FileOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      FileOutputStream fos = null;
      FileChannel fc = null;
      long pos;
      byte b[] = {65, 66, 67, 68, 69};
      
      try {
         // create new file output stream
         fos = new FileOutputStream("test.txt");
         
         // write buffer to the output stream
         fos.write(b);
         
         // pushes stream content to the underlying file
         fos.flush();
         
         // returns file channel associated with this stream
         fc = fos.getChannel();
         
         // returns the number of bytes written
         pos = fc.position();
         
         // prints
         System.out.print(pos);
         
      } catch(Exception ex) {
         // if an error occurs
         ex.printStackTrace();
      } finally {
      
         if(fos!=null)
            fos.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−

5
Example - Writing Data to a File Using FileChannel

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

FileOutputStreamDemo.java
package com.tutorialspoint;

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

public class FileOutputStreamDemo {
   public static void main(String[] args) {
      try (FileOutputStream fos = new FileOutputStream("output.txt")) {
         // Get FileChannel from FileOutputStream
         FileChannel fileChannel = fos.getChannel();

         // Create a ByteBuffer with data
         ByteBuffer buffer = ByteBuffer.allocate(64);
         buffer.put("Hello, FileChannel!".getBytes());

         // Flip the buffer to prepare for writing
         buffer.flip();

         // Write buffer contents to the file
         fileChannel.write(buffer);

         System.out.println("Data written to file using FileChannel.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Output

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

Data written to file using FileChannel.
Explanation Example - Locking a File Using FileChannel

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

FileOutputStreamDemo.java
package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class FileOutputStreamDemo {
   public static void main(String[] args) {
      try (FileOutputStream fos = new FileOutputStream("lockedFile.txt")) {
         // Get FileChannel
         FileChannel fileChannel = fos.getChannel();

         // Try to acquire an exclusive lock on the file
         FileLock lock = fileChannel.lock();
         System.out.println("File is locked for writing.");

         // Simulate some file operation
         Thread.sleep(5000);

         // Release the lock
         lock.release();
         System.out.println("File lock released.");
      } catch (IOException | InterruptedException e) {
         e.printStackTrace();
      }
   }
}
Output()

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

File is locked for writing.
File lock released.
Explanation

java_io_fileoutputstream.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