FileChannel Map Example
This program demonstrates mapping a file into memory using FileChannel.
Problem Statement
Write a Java program that maps a file into memory using FileChannel.map().
Source Code
| 1 | import java.io.*; |
| 2 | import java.nio.*; |
| 3 | import java.nio.channels.*; |
| 4 | import java.nio.file.*; |
| 5 | public class FileChannelMapExample { |
| 6 | public static void main(String[] args) throws Exception { |
| 7 | System.out.println("Eduinq FileChannel Map Example"); |
| 8 | FileChannel channel=FileChannel.open(Paths.get("map.txt"),StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE); |
| 9 | MappedByteBuffer buffer=channel.map(FileChannel.MapMode.READ_WRITE,0,1024); |
| 10 | buffer.put("Eduinq Memory Map".getBytes()); |
| 11 | channel.close(); |
| 12 | System.out.println("Eduinq File Mapped Successfully"); |
| 13 | } |
| 14 | } |
Program Output
Eduinq FileChannel Map Example Eduinq File Mapped Successfully
Explanation
Uses FileChannel.map() for memory mapping. - Provides direct access to file via buffer. - Exception handling ensures safe operations. - In enterprise systems, mapping supports high speed I/O. Conclusion: Shows mapping file with FileChannel.