Random Access File Example
This program demonstrates reading and writing using random access files.
Problem Statement
Write a Java program that uses RandomAccessFile to read and write data.
Source Code
| 1 | import java.io.*; |
| 2 | public class RandomAccessExample { |
| 3 | public static void main(String[] args) { |
| 4 | System.out.println("Eduinq Random Access Example"); |
| 5 | try(RandomAccessFile raf=new RandomAccessFile("random.txt","rw")) { |
| 6 | raf.writeUTF("Eduinq Random Access"); |
| 7 | raf.seek(0); |
| 8 | String data=raf.readUTF(); |
| 9 | System.out.println("Eduinq Data: "+data); |
| 10 | } catch(IOException e) { |
| 11 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 12 | } |
| 13 | } |
| 14 | } |
Program Output
Eduinq Random Access Example Eduinq Data: Eduinq Random Access
Explanation
Random access allows reading/writing at specific positions. - Useful for updating files without rewriting entire content. - Exception handling ensures safe operations. - In enterprise systems, random access is used for databases and logs. Conclusion: Shows random access file handling.