RandomAccessFile Pointer Example
This program demonstrates using file pointer in RandomAccessFile.
Problem Statement
Write a Java program that displays current file pointer position using RandomAccessFile.
Source Code
| 1 | import java.io.*; |
| 2 | public class RandomPointerExample { |
| 3 | public static void main(String[] args) { |
| 4 | System.out.println("Eduinq RandomAccessFile Pointer Example"); |
| 5 | try(RandomAccessFile raf=new RandomAccessFile("sample.txt","r")) { |
| 6 | System.out.println("Eduinq Initial Pointer: "+raf.getFilePointer()); |
| 7 | raf.seek(15); |
| 8 | System.out.println("Eduinq Pointer After Seek: "+raf.getFilePointer()); |
| 9 | } catch(Exception e){ System.out.println("Eduinq Exception: "+e.getMessage()); } |
| 10 | } |
| 11 | } |
Program Output
Eduinq RandomAccessFile Pointer Example Eduinq Initial Pointer: 0 Eduinq Pointer After Seek: 15
Explanation
Uses getFilePointer() to show position. - Updates pointer after seek. - Exception handling ensures safe operations. - In enterprise systems, pointer tracking supports file navigation. Conclusion: Shows file pointer usage in RandomAccessFile.