import java.io.*;
public class RandomAccesExample {
	final  int CHAR_POSITION = 2;
	static String fileName = "Random.txt";
	public static void main(String[] args) {
		try {
			RandomAccesExample random = new RandomAccesExample();
			random.writeAlpha();
			random.readAlpha();
		}
		catch (Exception e){
				System.out.println(e);
		}
	}
	public void readAlpha ()throws IOException {
		try {
			File dataFile = new File (fileName);
			java.io.RandomAccessFile raFile = new java.io.RandomAccessFile(dataFile, "r");
			System.out.println("/nRandom Alphabets from Random.txt:");
			long length = raFile.length();
			for (int i = CHAR_POSITION; i < length; i += 2 *CHAR_POSITION){
				raFile.seek(i);
				System.out.println(raFile.readChar());
			}
			raFile.close();
		}
		catch(FileNotFoundException e){
			System.out.println("File not found");
			System.exit(0);
		}
		catch (IOException e){
			System.out.println(e);
		}
	}
	public void writeAlpha()throws IOException {
		File dataFile = new File (fileName);
		java.io.RandomAccessFile raFile = new java.io.RandomAccessFile (dataFile, "rw");
		System.out.println("The data written to file" + "Random.txt are:");
		for (int i = 65; i < 91; i++){
			raFile.writeChar(i);
			System.out.print((char) i + " ");
		}
		System.out.println();
		raFile.close();
	}
}
