Write a Java program to track how many times the program has
Write a Java program to track how many times the program has been executed. You can store an int representing the count in a file, e.g., count.dat. Each time your program is executed, read the count, increment it by 1, and then store it back to the file. If count.dat doesn’t exist (e.g., first time to run the program), store 1 in the file. Use binary IO for your code. For this question, just write your code in the main method and declare the checked exceptions in the method’s header.
Solution
Let the program be Exercise_1 and store the count in Exercise_1.dat
package Chapter1
import java.io.File;
import java.io.RandomAccessFile;
import java.io.IOException;
public class Exercise_1
{
public static void main(String args[]) throws IOException
{
File file=new File(“Exercise_1.dat”);
try(RandomAccessFile raf=new RandomAccessFile(file,”rw”))
{
int count = (raf.length()!=0)? Raf.readInt()+1:1;
raf.seek(0);
raf.writeInt(count);
System.out.println(“Count is ”+count);
}
}
}

