
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - RandomAccessFile getChannel() method
Description
The Java RandomAccessFile getChannel() method returns the unique FileChannel object associated with this file.
getChannel() method −
Returns the FileChannel object associated with the RandomAccessFile.
The FileChannel can be used for advanced file I/O operations like memory-mapped files, locking, reading/writing via buffers, and more.
You can use it for both reading and writing, depending on the mode ("r" or "rw").
Declaration
Following is the declaration for java.io.RandomAccessFile.getChannel() method.
public final FileChannel getChannel()
Parameters
NA
Return Value
This method returns the file channel associated with this file.
Exception
NA
Example - Usage of RandomAccessFile getChannel() method
The following example shows the usage of RandomAccessFile getChannel() method.
RandomAccessFileDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.RandomAccessFile; public class RandomAccessFileDemo { public static void main(String[] args) { try { // create a new RandomAccessFile with filename test RandomAccessFile raf = new RandomAccessFile("test.txt", "rw"); // write something in the file raf.writeUTF("Hello World"); // set the file pointer at 0 position raf.seek(0); // read and print the contents of the file System.out.println("" + raf.readUTF()); // return the channel of the file System.out.println("" + raf.getChannel()); // close the strea and release resources raf.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
Output
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 −
ABCDE
Let us compile and run the above program, this will produce the following result −
Hello World sun.nio.ch.FileChannelImpl@3d4b7453
Example - Getting the size of the file using getChannel()
The following example shows the usage of RandomAccessFile getChannel() method.
RandomAccessFileDemo.java
package com.tutorialspoint; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; import java.io.IOException; public class RandomAccessFileDemo { public static void main(String[] args) { try (RandomAccessFile raf = new RandomAccessFile("example1.txt", "rw")) { raf.writeUTF("Hello, FileChannel!"); FileChannel channel = raf.getChannel(); long size = channel.size(); // Get the size of the file System.out.println("Size of file (in bytes): " + size); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Size of file (in bytes): 24
Explanation
RandomAccessFile writes a UTF string to a file.
getChannel() returns the FileChannel associated with it.
The channel is used to get the file size using channel.size().
Example - Positioning the file pointer using FileChannel.position()
The following example shows the usage of RandomAccessFile getChannel() method.
RandomAccessFileDemo.java
package com.tutorialspoint; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; import java.io.IOException; import java.nio.ByteBuffer; public class RandomAccessFileDemo { public static void main(String[] args) { try (RandomAccessFile raf = new RandomAccessFile("example2.txt", "rw")) { raf.writeUTF("1234567890"); FileChannel channel = raf.getChannel(); channel.position(2); // Move to 2nd byte (inside UTF string) ByteBuffer buffer = ByteBuffer.allocate(8); int bytesRead = channel.read(buffer); // Read 8 bytes from position 2 buffer.flip(); System.out.print("Data read from position 2: "); while (buffer.hasRemaining()) { System.out.print((char) buffer.get()); } } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Data read from position 2: 12345678
Explanation
Writes data using RandomAccessFile.
Uses the FileChannel to move the file pointer to a specific position and read from there using a ByteBuffer.
Demonstrates how to perform non-sequential (random) file access with getChannel().