
In a random access file, first write the alphabets a,b, c, d till z. Read the file in reverse order and print it on screen.

The given code uses RandomAccessFile file to write the alphabets from A to Z into the textfile. After storing the alphabets into the file, it then read the file in reverse order and display it on console.
import java.io.*;
class RandomAccessFileExample
{
public static void main(String[] args)
{
try{
File f=new File("c:/alphabet.txt");
RandomAccessFile randomFile=new RandomAccessFile(f,"rw");
for(int i=65;i<=90;i++){
randomFile.writeBytes(Character.toString((char)i));
}
randomFile.close();
RandomAccessFile raf = new RandomAccessFile(f, "r");
long position = raf.length();
while (position > 0) {
position -= 1;
raf.seek(position);
byte b = raf.readByte();
System.out.print((char) b);
}
}
catch(Exception e){}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.