Question: LAB13 Instructions The objective for this lab is for you to modify the following 2 programs: ReadBinaryFile ReadUTF so that they do not fault when

LAB13 Instructions

The objective for this lab is for you to modify the following 2 programs:

ReadBinaryFile

ReadUTF

so that they do not fault when they reach the end of the file. You will need to add in appropriate try and catch statements to gracefully handle an end of file exception.

In JGrasp, create a project called LAB13:

  • Download ReadBinaryFile.java, ReadUTF.java, Numbers.dat and UTFnames.dat from BB, open in JGrasp and save in project LAB13.
  • Make changes to ReadBinaryFile and ReadUTF so that they do not fault when they reach the end of the file.

Your submission to BB should be your ReadBinaryFile.java and ReadUTF.java files only, Not included in a .zip file or a .gpj file.

//ReadBinaryFile.java//

import java.io.*; /** * This program opens a binary file, reads and displays the contents. */ public class ReadBinaryFile { public static void main(String[] args) throws IOException { int number; // A number read from the file // Create the binary file input objects. FileInputStream fstream = new FileInputStream("Numbers.dat"); DataInputStream inputFile = new DataInputStream(fstream); System.out.println("Reading numbers from the file:"); for (int i = 0; i < 10; i++) { number = inputFile.readInt(); System.out.print(number + " "); } System.out.println(" Done."); // Close the file. inputFile.close(); } }

//ReadUTF.java//

/** * This program reads UTF-8 encoded strings from a binary file. */ import java.io.*; public class ReadUTF { public static void main(String[] args) throws IOException { String name; // Create the input objects. FileInputStream fstream = new FileInputStream("UTFnames.dat"); DataInputStream inputFile = new DataInputStream(fstream); System.out.println("Reading the names from the file:"); // Read the contents of the file. for (int i = 0; i < 10; i++) { name = inputFile.readUTF(); System.out.print(name + " "); } System.out.println(" Done."); // Close the file. inputFile.close(); } }

You will need to add in appropriate try and catch statements to gracefully handle an end of file exception.

I have the "Numbers.dat" and UTFnames.dat files...

Just Add try { } and catch { } statements and then i will compile and check it

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!