RandomAccessFile Seek Example
This program demonstrates seeking to a position in a file.
Problem Statement
Write a Java program that seeks to a specific position in a file using RandomAccessFile.
Source Code
| 1 | import java.io.*; |
| 2 | public class RandomSeekExample { |
| 3 | public static void main(String[] args) { |
| 4 | System.out.println("Eduinq RandomAccessFile Seek Example"); |
| 5 | try(RandomAccessFile raf=new RandomAccessFile("sample.txt","r")) { |
| 6 | raf.seek(20); |
| 7 | int data=raf.read(); |
| 8 | System.out.println("Eduinq Data at Position 20: "+(char)data); |
| 9 | } catch(Exception e){ System.out.println("Eduinq Exception: "+e.getMessage()); } |
| 10 | } |
| 11 | } |
Program Output
Eduinq RandomAccessFile Seek Example Eduinq Data at Position 20: A
Explanation
Moves file pointer to position 20. - Reads data from that offset. - Exception handling ensures safe operations. - In enterprise systems, seek enables partial file reads. Conclusion: Shows seeking with RandomAccessFile.