/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/Exercise17_01.java
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.calebfontenot.mp5.files_calebfontenot;

import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;

/**
 *
 * @author caleb
 */
public class Exercise17_01 {

    public static void main(String[] args)
    {
        try (RandomAccessFile fileIO = new RandomAccessFile("Exercise17_01.txt", "rw")) {
            int random = 0;
            System.out.println("Writing data to file...");
            for (int i = 1; i < 100 + 1; ++i) {
                random = (int) (Math.random() * 9) + 1;
                System.out.print(random + " ");
                if (i != 0 && i % 10 == 0) {
                    System.out.println();
                }
                fileIO.writeInt(random);
            }
            System.out.println("Wrote to the file successfully!");
            System.out.println("File contents:");
            fileIO.seek(0);
            int readIterator = 1;
            while (true) {
                try {
                    System.out.print(fileIO.readInt() + " ");
                    if (readIterator != 0 && readIterator % 10 == 0) {
                        System.out.println();
                    }
                    ++readIterator;
                } catch (EOFException e) {
                    break;
                }

            }

        } catch (Exception ex) {
            System.out.println(ex);
        }

    }
}