MappedByteBuffer Write Example
This program demonstrates writing to a file using MappedByteBuffer.
Problem Statement
Write a Java program that writes data into a file using MappedByteBuffer.
Source Code
| 1 | import java.io.*; |
| 2 | import java.nio.*; |
| 3 | import java.nio.channels.*; |
| 4 | public class MappedWriteExample { |
| 5 | public static void main(String[] args) { |
| 6 | System.out.println("Eduinq MappedByteBuffer Write Example"); |
| 7 | try(RandomAccessFile raf=new RandomAccessFile("output.txt","rw"); |
| 8 | FileChannel fc=raf.getChannel()) { |
| 9 | MappedByteBuffer mbb=fc.map(FileChannel.MapMode.READ_WRITE,0,1024); |
| 10 | mbb.put("Eduinq Mapped Write".getBytes()); |
| 11 | System.out.println("Eduinq Data Written Successfully"); |
| 12 | } catch(Exception e){ System.out.println("Eduinq Exception: "+e.getMessage()); } |
| 13 | } |
| 14 | } |
Program Output
Eduinq MappedByteBuffer Write Example Eduinq Data Written Successfully
Explanation
Uses MappedByteBuffer for memory-mapped write. - Writes directly into memory region. - Exception handling ensures safe operations. - In enterprise systems, mapped write supports high-speed updates. Conclusion: Shows writing with MappedByteBuffer.