RandomAccessFile Log Update Example

This program demonstrates updating log files using RandomAccessFile.

Problem Statement

Write a Java program that updates a log file entry using RandomAccessFile.

Source Code

1 import java.io.*;
2 public class RandomLogExample {
3 public static void main(String[] args) {
4 System.out.println("Eduinq RandomAccessFile Log Example");
5 try(RandomAccessFile raf=new RandomAccessFile("log.txt","rw")) {
6 raf.seek(raf.length());
7 raf.writeBytes("\nEduinq Log Entry Added");
8 System.out.println("Eduinq Log Updated Successfully");
9 } catch(Exception e){ System.out.println("Eduinq Exception: "+e.getMessage()); }
10 }
11 }

Program Output

					Eduinq RandomAccessFile Log Example
Eduinq Log Updated Successfully
				

Explanation

Seeks to end of file. - Appends new log entry. - Exception handling ensures safe operations. - In enterprise systems, random access updates logs dynamically. Conclusion: Shows practical log update with RandomAccessFile.

Quick Links to Explore