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.
DeclarationFollowing is the declaration for java.io.FileOutputStream.getChannel() method −
public FileChannel getChannel()Parameters
NA
Return ValueThis method returns the file channel associated with this file output stream.
ExceptionIOException− If an I/O error occurs.
Example - Usage of FileOutputStream getChannel() methodThe following example shows the usage of Java FileOutputStream getChannel() method.
FileOutputStreamDemo.javapackage 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−
5Example - Writing Data to a File Using FileChannel
The following example shows the usage of Java FileOutputStream getChannel() method.
FileOutputStreamDemo.javapackage 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
A FileOutputStream object is created for output.txt.
The getChannel() method is used to get the FileChannel.
A ByteBuffer is created, filled with data, and flipped to prepare for writing.
The buffer is written to the file using fileChannel.write(buffer).
The try-with-resources statement ensures the file stream is closed properly.
The following example shows the usage of Java FileOutputStream getChannel() method.
FileOutputStreamDemo.javapackage 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
A FileOutputStream object is created for lockedFile.txt.
The getChannel() method retrieves the associated FileChannel.
fileChannel.lock() is called to acquire an exclusive lock on the file.
The program holds the lock for 5 seconds (simulating a file operation).
After 5 seconds, the lock is released using lock.release().
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